Advertisement
HowToRoblox

TrickOrTreatHandler

Oct 19th, 2021
1,394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.00 KB | None | 0 0
  1. local dss = game:GetService("DataStoreService")
  2. local ds = dss:GetDataStore("TrickOrTreat")
  3.  
  4.  
  5. local playersHouses = {}
  6.  
  7.  
  8. function saveData(p)
  9.    
  10.     local candy = p.leaderstats.Candy.Value
  11.     local houses = playersHouses[p]
  12.    
  13.     pcall(function()
  14.         ds:SetAsync(p.UserId, {candy, houses})
  15.     end)
  16. end
  17.  
  18.  
  19. game.Players.PlayerAdded:Connect(function(p)
  20.    
  21.    
  22.     local ls = Instance.new("Folder", p)
  23.     ls.Name = "leaderstats"
  24.    
  25.     local candy = Instance.new("IntValue", ls)
  26.     candy.Name = "Candy"
  27.    
  28.    
  29.     local candyData = 0
  30.     local housesData = {}
  31.    
  32.  
  33.         pcall(function()
  34.        
  35.         local data = ds:GetAsync(p.UserId)
  36.        
  37.         candyData = data[1]
  38.         housesData = data[2]
  39.     end)
  40.     playersHouses[p] = housesData
  41.    
  42.     candy.Value = candyData
  43. end)
  44.  
  45.  
  46.  
  47. game.Players.PlayerRemoving:Connect(saveData)
  48.  
  49. game:BindToClose(function()
  50.  
  51.     for i, p in pairs(game.Players:GetPlayers()) do
  52.  
  53.         spawn(saveData(p))
  54.     end
  55. end)
  56.  
  57.  
  58.  
  59. for i, house in pairs(script.Parent:GetChildren()) do
  60.    
  61.     if house:IsA("Model") and house:FindFirstChild("Doorbell") then
  62.        
  63.         local doorOpen = false
  64.        
  65.         local cd = house.Doorbell.ClickDetector
  66.        
  67.        
  68.         cd.MouseClick:Connect(function(p)
  69.            
  70.             if not table.find(playersHouses[p], house.Name) and not doorOpen then
  71.                
  72.                 doorOpen = true
  73.                
  74.                
  75.                 local character = game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())].Character
  76.                 character.Archivable = true
  77.                 character = character:Clone()
  78.                
  79.                 character:SetPrimaryPartCFrame(house.Door.CFrame - house.Door.CFrame.LookVector * 2)
  80.                
  81.                 wait(math.random(1, 3))
  82.                
  83.                 character.Parent = workspace
  84.                
  85.                
  86.                 local cf = house.Door.CFrame
  87.                 house.Door.CFrame = cf * CFrame.Angles(0, math.rad(90), 0) + (cf.LookVector * house.Door.Size.X/2) + (cf.RightVector * house.Door.Size.X/2)
  88.                
  89.                
  90.                 p.leaderstats.Candy.Value += 1
  91.                
  92.                
  93.                 wait(5)
  94.                
  95.                
  96.                 house.Door.CFrame = cf
  97.                 character:Destroy()
  98.                 table.insert(playersHouses[p], house.Name)
  99.                 doorOpen = false
  100.             end
  101.         end)
  102.     end
  103. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement