Changing datastore saving from usernames to userids

Hi!

How would I change this script from saving via usernames to userids? Reason why I want to do this is so that players can change their username and their data saves.

Code:

		Dem
			Stats	
--]]
game.Players.PlayerRemoving:connect(function(player)
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

local statstorage = player:FindFirstChild("leaderstats"):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)


--[[
	Loadin'
		Dem
			Stats
--]]
game.Players.PlayerAdded:connect(function(player)
		local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
	
	player:WaitForChild("leaderstats")
	wait(1)
	local stats = player:FindFirstChild("leaderstats"):GetChildren()
	for i = 1, #stats do			
	stats[i].Value = datastore:GetAsync(stats[i].Name)
	print("stat numba "..i.." has been found")
		end
end)

Thanks!

just replace player.Name with player.UserId

Just tested that out. That didn’t work for some reason.

Just use CTRL + H and replace player.Name with player.UserId. You might have missed some.

Nope I didn’t miss anything.

Make sure its capital UserId not userid

Also dont replace every single.Name, just player.Name

Okay I changed the stats[i] and statstorage[i] to Name and I also changed userid to UserId and it worked, thanks!