So, I was wondering if my DataStore will only save data if the player doesn't get their username changed. How can I fix this? I was wondering if I should acquire their user ID instead of their username but I'm not sure how to do this.
game.Players.PlayerRemoving:connect(function(player) local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Save") -- ?? local statstorage = player:FindFirstChild("PlayerStats"):GetChildren() for i = 1, #statstorage do datastore:SetAsync(statstorage[i].Name, statstorage[i].Value) print("saved data number "..i) end print("Stats successfully saved") end) game.Players.PlayerAdded:connect(function(player) local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Save") -- ?? player:WaitForChild("PlayerStats") wait(1) local stats = player:FindFirstChild("PlayerStats"):GetChildren() for i = 1, #stats do stats[i].Value = datastore:GetAsync(stats[i].Name) print("stat numba "..i.." has been found") end end)
No, this won't work with name changes. Typically Datastores are set with a Key that includes the user's id. In order to get their id, you simply need to use something like:
local key = player.UserId
Hope this helps, if you have any further questions let me know.
Dont use player name use playerid because the playername can be chainged but playerid cant.