Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Handle data
- local dss = game:GetService("DataStoreService")
- local ds = dss:GetDataStore("CandyData")
- function saveData(plr)
- local candy = plr.leaderstats.Candy.Value
- ds:SetAsync(plr.UserId, candy)
- end
- game.Players.PlayerRemoving:Connect(saveData)
- game:BindToClose(function()
- for i, plr in pairs(game.Players:GetPlayers()) do
- saveData(plr)
- end
- end)
- game.Players.PlayerAdded:Connect(function(plr)
- local ls = Instance.new("Folder")
- ls.Name = "leaderstats"
- ls.Parent = plr
- local candyVal = Instance.new("IntValue")
- candyVal.Name = "Candy"
- candyVal.Parent = ls
- local candyData = ds:GetAsync(plr.UserId) or 0
- candyVal.Value = candyData
- end)
- --Make houses work
- local housesFolder = workspace:WaitForChild("Houses")
- local trickOrTreatCooldown = {}
- function handleHouse(house)
- local door = house.Door
- local bell = house.Doorbell
- local clickDetector = Instance.new("ClickDetector", bell)
- local houseCooldowns = {}
- clickDetector.MouseClick:Connect(function(plr)
- local char = plr.Character
- if char and char:FindFirstChild("HumanoidRootPart") then
- local distance = (char.HumanoidRootPart.Position - door.Position).Magnitude
- if distance <= 10 then
- if not houseCooldowns[plr] and not trickOrTreatCooldown[plr] then
- trickOrTreatCooldown[plr] = true
- houseCooldowns[plr] = true
- game.ReplicatedStorage.TrickOrTreatReplicatedStorage.RemoteEvent:FireClient(plr, house)
- task.wait(6)
- plr.leaderstats.Candy.Value += 1
- trickOrTreatCooldown[plr] = false
- task.wait(20)
- houseCooldowns[plr] = false
- end
- end
- end
- end)
- end
- for i, house in pairs(housesFolder:GetChildren()) do
- handleHouse(house)
- end
- housesFolder.ChildAdded:Connect(handleHouse)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement