Advertisement
HowToRoblox

TrickOrTreatServer

Sep 22nd, 2022
1,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.80 KB | None | 0 0
  1. --Handle data
  2. local dss = game:GetService("DataStoreService")
  3. local ds = dss:GetDataStore("CandyData")
  4.  
  5. function saveData(plr)
  6.    
  7.     local candy = plr.leaderstats.Candy.Value
  8.    
  9.     ds:SetAsync(plr.UserId, candy)
  10. end
  11.  
  12. game.Players.PlayerRemoving:Connect(saveData)
  13.  
  14. game:BindToClose(function()
  15.     for i, plr in pairs(game.Players:GetPlayers()) do
  16.        
  17.         saveData(plr)
  18.     end
  19. end)
  20.  
  21. game.Players.PlayerAdded:Connect(function(plr)
  22.  
  23.     local ls = Instance.new("Folder")
  24.     ls.Name = "leaderstats"
  25.     ls.Parent = plr
  26.    
  27.     local candyVal = Instance.new("IntValue")
  28.     candyVal.Name = "Candy"
  29.     candyVal.Parent = ls
  30.    
  31.     local candyData = ds:GetAsync(plr.UserId) or 0
  32.     candyVal.Value = candyData
  33. end)
  34.  
  35.  
  36. --Make houses work
  37. local housesFolder = workspace:WaitForChild("Houses")
  38.  
  39. local trickOrTreatCooldown = {}
  40.  
  41.  
  42. function handleHouse(house)
  43.    
  44.     local door = house.Door
  45.     local bell = house.Doorbell
  46.    
  47.     local clickDetector = Instance.new("ClickDetector", bell)
  48.    
  49.     local houseCooldowns = {}
  50.    
  51.    
  52.     clickDetector.MouseClick:Connect(function(plr)
  53.        
  54.         local char = plr.Character
  55.         if char and char:FindFirstChild("HumanoidRootPart") then
  56.            
  57.             local distance = (char.HumanoidRootPart.Position - door.Position).Magnitude
  58.             if distance <= 10 then
  59.            
  60.                 if not houseCooldowns[plr] and not trickOrTreatCooldown[plr] then
  61.                     trickOrTreatCooldown[plr] = true
  62.                     houseCooldowns[plr] = true
  63.                    
  64.                     game.ReplicatedStorage.TrickOrTreatReplicatedStorage.RemoteEvent:FireClient(plr, house)
  65.                    
  66.                     task.wait(6)
  67.                     plr.leaderstats.Candy.Value += 1
  68.                     trickOrTreatCooldown[plr] = false
  69.                    
  70.                     task.wait(20)
  71.                     houseCooldowns[plr] = false
  72.                 end
  73.             end
  74.         end
  75.     end)
  76. end
  77.  
  78.  
  79. for i, house in pairs(housesFolder:GetChildren()) do
  80.     handleHouse(house)
  81. end
  82. housesFolder.ChildAdded:Connect(handleHouse)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement