local data = game:GetService("DataStoreService"):GetDataStore("Saves") local settings={ groupid = 2903819 } game:GetService("Players").PlayerAdded:connect(function(player) local key = "user_"..player.UserId local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats" local rank = Instance.new("StringValue", leaderstats) rank.Name = "Rank" rank.Value = player:GetRoleInGroup(settings["groupid"]) local GetPlayerSave = data:GetAsync("user_"..player.UserId) local points = Instance.new("NumberValue", leaderstats) points.Value = 0 or GetPlayerSave[1] points.Name = "Saves" end) game:GetService("Players").PlayerRemoving:connect(function(player) local leaderstats = player:WaitForChild("leaderstats") local ToSave = (leaderstats.Saves.Value) data:SetAsync("user_"..player.UserId, ToSave) end)
Data stores are very complicated to manage as there is a lot of things that can error, here is a link that can explain the limitations.
We save data to a key which we then can use to access this saved data, the key itself can be anything and in some cases it is used to store game settings with pre defined keys. The player user id is added to the key so that we can make all keys unique to that user.
There are two format that we can use to save data with a key, 1st we can save the raw data to the key e.g. a string, the second format which is the one you have used it to save the data in a table ( though you need to use {} and not (). The data store cannot save objects, to save these we would need to process then into a saveable format. This is known as serialisation and deserialization.
Back to your code:-
local data = game:GetService("DataStoreService"):GetDataStore("Saves") local settings={ groupid = 2903819 } game:GetService("Players").PlayerAdded:connect(function(player) local key = "user_"..player.UserId local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats" local rank = Instance.new("StringValue", leaderstats) rank.Name = "Rank" rank.Value = player:GetRoleInGroup(settings["groupid"]) local GetPlayerSave = data:GetAsync("user_"..player.UserId) -- we currently do not know if the player has data so 'GetPlayerSave ' can be nil local points = Instance.new("NumberValue", leaderstats) points.Value = 0 or GetPlayerSave[1] -- as mentioned before we need to swap this logic around points.Name = "Saves" end) game:GetService("Players").PlayerRemoving:connect(function(player) local leaderstats = player:WaitForChild("leaderstats") local ToSave = (leaderstats.Saves.Value) -- we need to use a table here {} and not () data:SetAsync("user_"..player.UserId, ToSave) end)
Basic changes:-
local data = game:GetService("DataStoreService"):GetDataStore("Saves") local settings={ groupid = 2903819 } game:GetService("Players").PlayerAdded:connect(function(player) local key = "user_"..player.UserId local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats" local rank = Instance.new("StringValue", leaderstats) rank.Name = "Rank" rank.Value = player:GetRoleInGroup(settings["groupid"]) local GetPlayerSave = data:GetAsync("user_"..player.UserId) local points = Instance.new("NumberValue", leaderstats) if GetPlayerSave then points.Value = GetPlayerSave[1] or 0 -- we may have data but no value so its best to check else points.Value = 0 -- no player data end points.Name = "Saves" end) game:GetService("Players").PlayerRemoving:connect(function(player) local leaderstats = player:WaitForChild("leaderstats") local ToSave = {leaderstats.Saves.Value} -- table format data:SetAsync("user_"..player.UserId, ToSave) end)
This includes the basics of what needed to change, this currently does not manage any other exceptions that may be thrown by the data store. Lastly there are change that can be made to simplify this code.
I hope this helps, please comment if you do not understand how / why this code works.