GaryScripts

Datastore Method for Fighting Game

Jun 17th, 2021
1,028
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local Datastore = game:GetService("DataStoreService"):GetDataStore("plrData")
  2.  
  3. game.Players.PlayerAdded:Connect(function(p)
  4.     local leaderstats = Instance.new("Folder", p)
  5.     leaderstats.Name = "leaderstats"
  6.    
  7.     local Money = Instance.new("NumberValue", leaderstats)
  8.     Money.Name = "Money"
  9.     Money.Value = 50
  10.    
  11.     local Knockouts = Instance.new("IntValue", leaderstats)
  12.     Knockouts.Name = "KOs"
  13.    
  14.     local Wipeouts = Instance.new("IntValue", leaderstats)
  15.     Wipeouts.Name = "Deaths"
  16.    
  17.     local savedData
  18.     local success, errMsg = pcall(function()
  19.         savedData = Datastore:GetAsync(p.UserId.."-Money")
  20.     end)
  21.    
  22.     if success then
  23.         p.leaderstats.Money.Value = savedData
  24.     else
  25.         warn(errMsg)
  26.     end
  27.    
  28.     p.CharacterAdded:Connect(function(char)
  29.         char.Humanoid.Died:Connect(function(killed)
  30.             local CreatorTag = char.Humanoid:FindFirstChild("creator")
  31.            
  32.             if CreatorTag and CreatorTag.Value then
  33.                 local leaderstatsPlr = CreatorTag.Value:WaitForChild("leaderstats")
  34.                
  35.                 leaderstatsPlr["Money"].Value += 20
  36.                 leaderstatsPlr["KOs"].Value += 1
  37.             end
  38.         end)
  39.     end)
  40.    
  41.    
  42. end)
  43.  
  44. game.Players.PlayerRemoving:Connect(function(p)
  45.     local success, errMsg = pcall(function()
  46.         Datastore:SetAsync(p.UserId.."-Money", p.leaderstats.Money.Value)
  47.     end)
  48.    
  49.     if success then
  50.         print(p.UserId.."'s data was successfully saved.")
  51.     else
  52.         warn(errMsg)
  53.     end
  54. end)
RAW Paste Data