Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local RS = game:GetService("ReplicatedStorage")
- local DSS = game:GetService("DataStoreService")
- local Data = DSS:GetDataStore("Save1")
- local SHOPCOSTS = {
- HealthPotion = 100;
- SpeedPotion = 200;
- SuperHealthPotion = 500;
- SuperSpeedPotion = 1000;
- }
- local Stats = {}
- local Deb = {}
- local function ChangeMoney(Player, Money)
- Stats[Player].Money = Money
- Player.leaderstats.Money.Value = tostring(Money)
- end
- local function playerJoined(Player)
- Stats[Player] = {}
- Deb[Player] = {}
- local leaderstats = Instance.new("Folder", Player)
- leaderstats.Name = "leaderstats"
- local Money = Instance.new("StringValue", leaderstats)
- Money.Name = "Money"
- local success, Save = pcall(function()
- return Data:GetAsync(Player.UserId)
- end)
- if success then
- if Save then
- ChangeMoney(Player, Save.Money)
- else
- ChangeMoney(Player, 10000)
- end
- else
- Player:Kick("Failed to load data. Please Rejoin")
- end
- end
- local function playerRemoving(Player)
- if Stats[Player] and Stats[Player].Loaded then
- Data:SetAsync(Player.UserId, Stats[Player])
- end
- Stats[Player] = nil
- Deb[Player] = nil
- end
- local function Heals(Player, Heal)
- coroutine.wrap(function()
- Deb[Player].Health = true
- local Loop = 0
- while wait(1) and Loop <= 10 do
- Player.Character.Humanoid.Health += Heal
- Loop = Loop + 1
- end
- Deb[Player].Health = nil
- end)()
- end
- local function Speeds(Player, Speed)
- coroutine.wrap(function()
- Deb[Player].Speed = true
- Player.Character.Humanoid.WalkSpeed = Speed
- delay(10, function()
- Player.Character.Humanoid.WalkSpeed = 16
- Deb[Player].Speed = nil
- end)
- end)()
- end
- local function potionActivated(Player, Potion)
- local PotionInstance = Player.Character:FindFirstChild(Potion) or Player.Backpack:FindFirstChild(Potion)
- if not PotionInstance then return end
- if Potion == "HealthPotion" then
- if Deb[Player].Health then return end
- local HEAL_PER_SECOND = 2
- Heals(Player, HEAL_PER_SECOND)
- elseif Potion == "SuperHealthPotion" then
- if Deb[Player].Health then return end
- local HEAL_PER_SECOND = 5
- Heals(Player, HEAL_PER_SECOND)
- elseif Potion == "SpeedPotion" then
- if Deb[Player].Speed then return end
- local SPEED = 20
- Speeds(Player, SPEED)
- elseif Potion == "SuperSpeedPotion" then
- if Deb[Player].Speed then return end
- local SPEED = 26
- Speeds(Player, SPEED)
- end
- PotionInstance:Destroy()
- end
- local function shopItemBuyer(Player, Item)
- local COST = SHOPCOSTS[Item]
- local ITEMINSTANCE = RS.ShopItems:FindFirstChild(Item)
- if not COST or not ITEMINSTANCE then return end
- if Stats[Player].Money < COST then return end
- ChangeMoney(Player, Stats[Player].Money - COST)
- local Clone = ITEMINSTANCE:Clone()
- Clone.Parent = Player.Backpack
- end
- game.Players.PlayerAdded:Connect(playerJoined)
- game.Players.PlayerRemoving:Connect(playerRemoving)
- RS.Events.potionActivation.OnServerEvent:Connect(potionActivated)
- RS.Events.buyShopItem.OnServerEvent:Connect(shopItemBuyer)
- game:BindToClose(function()
- if game["Run Service"]:IsStudio() then
- return
- end
- for i,v in pairs(game.Players:GetPlayers()) do
- playerRemoving(v)
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement