Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Will this DataStore still save data even if the player changes their username?

Asked by 6 years ago

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)

2 answers

Log in to vote
3
Answered by
dyler3 1510 Moderation Voter
6 years ago

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.

Ad
Log in to vote
0
Answered by 6 years ago

Dont use player name use playerid because the playername can be chainged but playerid cant.

Answer this question