Here's my script:
local DSS = game:GetService("DataStoreService")
local myDSS = DSS:GetDataStore("Store1")
game.Players.PlayerAdded:connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "Stats"
stats.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = stats
local Gems = Instance.new("IntValue")
Gems.Name = "Gems"
Gems.Parent = stats
local Damage = Instance.new("IntValue")
Damage.Name = "Damage"
Damage.Parent = stats
local Speed = Instance.new("IntValue")
Speed.Name = "Speed"
Speed.Parent = stats
local Health = Instance.new("IntValue")
Health.Name = "Health"
Health.Parent = stats
local playerUserId = player.UserId
local data
local success, errormessage = pcall(function()
data = myDSS:GetAsync(playerUserId)
end)
if success then
if data
then
Coins.Value = data[1]
Gems.Value = data[2]
Damage.Value = data[3]
Health.Value = data[4]
Speed.Value = data[5]
print(player.Name.." has Coins, Gems, Damage, Health, Speed: "..table.concat(data, ","))
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = player.UserId
local data = {player.Stats.Coins.Value,player.Stats.Gems.Value, player.Stats.Damage.Value,player.Stats.Health.Value,player.Stats.Speed.Value}
local success, errormessage = pcall(function()
myDSS:SetAsync(playerUserId, data)
end)
if success then
print("Data Saved Success!")
else
print ("There was an error!")
warn(errormessage)
end
end)
game.Workspace.Moneypart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid")
then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.Stats.Gems.Value = player.Stats.Gems.Value +1
end
end)
Thanks in advance!

2 comments
u should put your saving script in a function and loop it with while wait(45)do
Use "game:BindToClose()" to save all the player's data when the game gets closed.