Advertisement
HowToRoblox

EmotesServer

Oct 1st, 2022 (edited)
2,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.75 KB | None | 0 0
  1. local dss = game:GetService("DataStoreService")
  2. local ds = dss:GetDataStore("EmotesDS")
  3.  
  4.  
  5. local repl = game.ReplicatedStorage:WaitForChild("EmotesReplicatedStorage")
  6. local re = repl:WaitForChild("RE")
  7. local allEmotes = repl:WaitForChild("Emotes")
  8.  
  9.  
  10. --Saving data
  11. function save(plr)
  12.    
  13.     local cash = plr.leaderstats.Cash.Value
  14.    
  15.     local ownedEmotes = plr.Emotes:GetChildren()
  16.     local emoteNames = {}
  17.    
  18.     for i, emote in pairs(ownedEmotes) do
  19.         table.insert(emoteNames, emote.Name)
  20.     end
  21.    
  22.     local combinedData = {cash, emoteNames}
  23.    
  24.     ds:SetAsync(plr.UserId, combinedData)
  25. end
  26.  
  27. game.Players.PlayerRemoving:Connect(save)
  28.  
  29. game:BindToClose(function()
  30.    
  31.     for i, plr in pairs(game.Players:GetPlayers()) do
  32.         save(plr)
  33.     end
  34. end)
  35.  
  36.  
  37. --Loading data
  38. game.Players.PlayerAdded:Connect(function(plr)
  39.    
  40.     local ls = Instance.new("Folder")
  41.     ls.Name = "leaderstats"
  42.     ls.Parent = plr
  43.    
  44.     local cashV = Instance.new("IntValue")
  45.     cashV.Name = "Cash"
  46.     cashV.Parent = ls
  47.    
  48.    
  49.     local emotesF = Instance.new("Folder")
  50.     emotesF.Name = "Emotes"
  51.     emotesF.Parent = plr
  52.    
  53.    
  54.     local data = ds:GetAsync(plr.UserId) or {1000, {}}
  55.    
  56.     cashV.Value += data[1]
  57.    
  58.     for i, emote in pairs(data[2]) do
  59.        
  60.         if allEmotes:FindFirstChild(emote) then
  61.             allEmotes[emote]:Clone().Parent = emotesF
  62.         end
  63.     end
  64. end)
  65.  
  66.  
  67. --Handling purchases
  68. re.OnServerEvent:Connect(function(plr, emoteN)
  69.    
  70.     if emoteN and allEmotes:FindFirstChild(emoteN) and allEmotes[emoteN]:FindFirstChild("PRICE") then
  71.        
  72.         local price = allEmotes[emoteN].PRICE.Value
  73.         local pCash = plr.leaderstats.Cash
  74.        
  75.         local pEmotes = plr.Emotes
  76.        
  77.         if pCash.Value >= price and not pEmotes:FindFirstChild(emoteN) then
  78.             pCash.Value -= price
  79.            
  80.             allEmotes[emoteN]:Clone().Parent = pEmotes
  81.         end
  82.     end
  83. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement