Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Error message "attempt to index nil with UserId" in datastore, how can i fix it?

Im trying to make a datastore script but it wont save, it gives me the error message "attempt to index nil with UserId". I dont know how to fix it. Here is my code:

local datastoreservice = game:GetService("DataStoreService")
local datastorage = datastoreservice:GetDataStore("datastorage")
print("Variables loaded")

local function Load(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Nights = Instance.new("IntValue")
	Nights.Name = "Nights"
	Nights.Parent = leaderstats
	
	print("Leaderstats created")
	
	local data
	local success, errormessage = pcall(function()
		data = datastorage:GetAsync(player.UserId)
	end)
	print("Data load happened")
	
	if success then
		print("Data loaded")
	else
		warn("Error happened while loading data: "..errormessage)
	end
end

local function Save(player)
	local success, errormessage = pcall(function()
		datastorage:SetAsync(player.UserId, player.leaderstats.nights.Value)		
	end)	
	
	if success then
		print("Data saved")
	else
		warn("Error happened while saving data: "..errormessage)
	end
end

game.Players.PlayerAdded:Connect(Load)
game:BindToClose(Save)

Please help me, ive been having issues with datastoring for a few days

2 answers

✓ Accepted answer

Well, BindToClose won't give you the player, so when you try to save when the game closes, the variable player is basically nil, and so is the player.UserId. Instead of using a parameter, you can loop through the players (using a for loop) and save data for each player.

--Example:
for i, player in pairs(game:GetService("Players"):GetPlayers()) do --Loop through the players.
	--Save player data here.
end

1 comment

0

Thank you

It could be because your local functions each have a parameter, which is player. But when you fire your functions, you're not giving them a parameter, so player is nil.

Here's your fixed script:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
print("Variables Loaded")

function Load(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local nights = Instance.new("IntValue",leaderstats)
	nights.Name = "Nights"

	print("Leaderstats Loaded")

	local data

	local success,err = pcall(function()
		data = DataStore:GetAsync(player.UserId)
	end)
	
	if success then
		print("Data loaded!")
	else
		print("Error whilst loading player's data.")
	end
	nights.Value = data
end


function Save(player)
	local nights = player.leaderstats.Nights

	local success,err = pcall(function()
		DataStore:SetAsync(player.UserId, nights.Value)
	end)
	
	if success then
		print("Data saved!")
	else
		print("Error whilst saving player's data.")
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	Load(plr)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	Save(plr)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.