Also, if I don't have the spawn(function() it will just take forever for the game to close (on studio) and save the players data. And once it has finally closed it would say "Not running script because past shutdown deadline (x247)"
local DSS = game:GetService("DataStoreService"):GetDataStore("Testing")
local DefaultStats = {
Coins = 0,
Blocks = 0,
Rebirths = 0,
}
local function load(player)
local key = "user_"..player.UserId
local success, data = pcall(function()
return DSS:GetAsync(key)
end)
if(success)then
print("Loaded "..player.Name.."'s Data!")
if data == nil then
data = DefaultStats
end
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local Coins = Instance.new("IntValue",leaderstats)
Coins.Name = "Coins"
local Blocks = Instance.new("IntValue",leaderstats)
Blocks.Name = "Blocks"
local Rebirths = Instance.new("IntValue",leaderstats)
Rebirths.Name = "Rebirths"
Coins.Value = data.Coins
Blocks.Value = data.Blocks
Rebirths.Value = data.Rebirths
local CurrentRocket = Instance.new("StringValue",player)
CurrentRocket.Name = "Current_Rocket"
CurrentRocket.Value = "Rocket"
else
print("Couldn't Load "..player.Name.."'s Data!")
end
end
local function save(player)
local key = "user_"..player.UserId
local Items = {
Coins = player.leaderstats.Coins.Value,
Blocks = player.leaderstats.Blocks.Value,
Rebirths = player.leaderstats.Rebirths.Value,
}
local Success, message = pcall(function()
DSS:SetAsync(key,Items)
end)
if Success then
print("Saved "..player.Name.."'s Stats")
else
print("Failed to save "..player.Name.. "'s Stats")
end
end
game:BindToClose(function()
for _, player in ipairs(game.Players:GetPlayers()) do
spawn(function()
save(player)
end)
end
end)
game.Players.PlayerAdded:Connect(function(player)
load(player)
end)
game.Players.PlayerRemoving:Connect(function(player)
save(player)
end)

3 comments
Does it show on the output in errors?
DataStores are pretty tricky. Did you test it on a 2-player server?
The only errors it shows are that the data had been on a queue and that the script had passed shutdown deadline. I tried this in 2 player it says it saves but then it makes another datastore script not work.