1MinuteRoblox

LEADERSTATS AND DATASTORE SCRIPT by 1MinuteRobloxTutorials

Dec 30th, 2023 (edited)
1,382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. --Leaderstats Script by 1MinuteRobloxTutorials--
  2.  
  3. local Players = game.Players
  4.  
  5. Players.PlayerAdded:Connect(function(player)
  6.  
  7. local leaderstats = Instance.new("Folder")
  8. leaderstats.Name = "leaderstats"
  9. leaderstats.Parent = player
  10.  
  11. local Coins = Instance.new("IntValue")
  12. Coins.Name = "Coins"
  13. Coins.Value = 0
  14. Coins.Parent = leaderstats
  15.  
  16. local Deaths = Instance.new("IntValue")
  17. Deaths.Name = "Deaths"
  18. Deaths.Value = 0
  19. Deaths.Parent = leaderstats
  20.  
  21. wait(1)
  22. local Stats = leaderstats:Clone()
  23. Stats.Parent = player
  24. player.CharacterAdded:connect(function(Character)
  25. local Humanoid = Character:FindFirstChild "Humanoid"
  26. Deaths.Value = Deaths.Value + 1
  27. if Humanoid then
  28. Humanoid.Died:connect(function()
  29. for i, Child in pairs(Humanoid:GetChildren()) do
  30. if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
  31. local Killer = Child.Value
  32. if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
  33. local Kills = Killer.leaderstats.Kills
  34. Kills.Value = Kills.Value + 1
  35. end
  36. return
  37. end
  38. end
  39. end)
  40. end
  41. end)
  42. end)
  43.  
  44. -------------------------------------------------------------------------------------------------------------------------
  45.  
  46. --Data Store Script by 1MinuteRobloxTutorials
  47.  
  48. local DataStoreService = game:GetService("DataStoreService")
  49. local dataStore = DataStoreService:GetDataStore("DataStore")
  50.  
  51. game.Players.PlayerAdded:Connect(function(player)
  52. local leaderstats = player:WaitForChild("leaderstats")
  53.  
  54. local CoinsKey = player.UserId.."-Coins"
  55. local deathsKey = player.UserId.."-Deaths"
  56.  
  57. local Coins, deaths = 0, 0
  58.  
  59. local success, errorMessage = pcall(function()
  60. Coins = dataStore:GetAsync(CoinsKey) or 0
  61. deaths = dataStore:GetAsync(deathsKey) or 0
  62. end)
  63.  
  64. if success then
  65. leaderstats.Coins.Value = Coins
  66. leaderstats.Deaths.Value = deaths
  67. else
  68. warn("Failed to retrieve data: " .. errorMessage)
  69. end
  70.  
  71. player.Changed:Connect(function(property)
  72. if property == "Parent" and player.Parent == nil then
  73. local success, errorMessage = pcall(function()
  74. dataStore:SetAsync(CoinsKey, leaderstats.Coins.Value)
  75. dataStore:SetAsync(deathsKey, leaderstats.Deaths.Value)
  76. end)
  77.  
  78. if not success then
  79. warn("Failed to save data: " .. errorMessage)
  80. end
  81. end
  82. end)
  83. end)
  84.  
  85. -- Add this line to ensure data is saved when the game shuts down
  86. game:BindToClose(function()
  87. for _, player in ipairs(game.Players:GetPlayers()) do
  88. local leaderstats = player:FindFirstChild("leaderstats")
  89. if leaderstats then
  90. local CoinsKey = player.UserId.."-Coins"
  91. local deathsKey = player.UserId.."-Deaths"
  92.  
  93. local success, errorMessage = pcall(function()
  94. dataStore:SetAsync(CoinsKey, leaderstats.Coins.Value)
  95. dataStore:SetAsync(deathsKey, leaderstats.Deaths.Value)
  96. end)
  97.  
  98. if not success then
  99. warn("Failed to save data: " .. errorMessage)
  100. end
  101. end
  102. end
  103. end)
  104.  
  105.  
  106.  
  107.  
Advertisement
Add Comment
Please, Sign In to add comment