I'll just add to ParticularlyPenguin's answer. A lot of people also have this problem and it's that if you're playing solo in Roblox Studio, or you could also be the last player in a server, or the server could just shut down unexpectedly, but when you leave, the server may shut down before your functions such as PlayerRemoving finishes. If that function does not run then your player's data will not be saved. This is why it is always best to use a function called :BindToClose for saving data when a player leaves. It will allow all bound functions to run before the server shuts down. I've edited your script and you will see how it works. Lastly, on line 27, you did not capitalize "Points" in "player.leaderstats.Points.Value". "points" is not a valid member of leaderstats so it's important that you have the capitalization right.
local DSS = game:GetService("DataStoreService")
local myDataStore = DSS:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-points")
end)
if success then
cash.Value = data
else
print("There was an error while getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
data = myDataStore:SetAsync(player.UserId.."-points",player.leaderstats.Points.Value)
end)
if success then
print("Player Data successfully saved!")
else
print("There was an error when saving your data")
warn(errormessage)
end
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local success, errormessage = pcall(function()
data = myDataStore:SetAsync(player.UserId.."-points",player.leaderstats.Points.Value)
end)
if success then
print("Player Data successfully saved!")
else
print("There was an error when saving your data")
warn(errormessage)
end
end
end)
5 comments
Did you define "myDataStore"?
I dont think so, should i do local myDataStore = data?
Please show your ENTIRE script, thanks.
this is the entire script
Ohh.