: attempt to index nil with 'UserId'

Ok so i done this and have been playing with it for 2 days on and off just cant seem to see what im missing any help would be appreciated

ServerScriptService.ServerHandle.Data:107: attempt to index nil with ‘UserId’ - Server - Data:107

dataMod.set = function(player, stat, value)

local key = "Player_" .. player.UserId

sessionData [key] [stat] = value

player.leaderstats[stat].Value = value

end

Full Script

local dataMod = {}

local Players = game:GetService("Players")
local playerService = game:GetService("Players"):GetPlayerByUserId(1)
local dataService = game:GetService("DataStoreService")
local store = dataService:GetDataStore("DataStorev1")
local sessionData = {}
local dataMod = {}



dataMod.recursiveCopy = function(dataTable)
	local tableCopy = {}

	for index, value in pairs(dataTable) do
		if type(value) == "table" then
			value = dataMod.recursiveCopy(value)

		end
		tableCopy[index] = value
	end
	return tableCopy
end





local defaultData = {

	Coins = 110;
	Stage = 1;
}
--auto save---------------------

local AUTOSAVE_INTERVAL = 120
local function autoSave()
	while wait(AUTOSAVE_INTERVAL)do
		print("Auto-saving data for all players")
		for key, dataTable in pairs(sessionData) do
			local player = Players:GetPlayersByUserId(key)
			dataMod.save(player)
		end
	end
end
spawn(autoSave) --Initialize autosave loop


dataMod.setupData = function(player)
	local key = "Player_" .. player.UserId
	local data = dataMod.load(player)
	sessionData [key] = dataMod.recursiveCopy(defaultData)

	if data then
		for index, value in pairs(data) do 
			dataMod.set()
			print (index,value)
			dataMod.set(player, index,value)
		end
		print (player.Name.."`s data has been loaded!")
	else
		print (player.Name.."`s is a new player!")
	end

end





--saveing data-----------------------------------------
game:BindToClose(function()
	for _, player in pairs(Players:GetPlayers())do
		dataMod.save(player)
		player:kick("Shutting down game data saved.")
	end
end)







Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder")
	folder.Name= "leaderstats"	
	folder.Parent= player

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = folder
	coins.Value = defaultData.Coins	

	local stage = Instance.new ("IntValue")
	stage.Name = "Stage"
	stage.Parent = folder
	stage.Value = defaultData.Stage

	dataMod.setupData(player)


end)

dataMod.set = function(player, stat, value)
	local key = "Player_" .. player.UserId
	sessionData [key] [stat] = value
	player.leaderstats[stat].Value = value
end

dataMod.increment = function (player, stat, value)
	local key = "Player_" .. player.UserId
	sessionData[key][stat] = dataMod.get(player, stat)+ value

end

dataMod.get = function(player, stat)
	local key = "Player_" .. player.UserId
	return sessionData[key][stat]
end


dataMod.save = function(player)
	local key = "Player_" .. player.UserId
	local data = dataMod.recursiveCopy(sessionData[key])

	local success, err = pcall(function()
		store:SetAsync (key, data)
	end)
	if success then 
		print(player.Name.."`s data has been saved!")
	end
end

dataMod.load = function(player)
	local key = "Player_" .. player.UserId
	local data 

	local success, err = pcall(function()
		data = store:GetAsync (key)
	end)
	if not success then 
		dataMod.load(player)
	end

	print(player.Name.."`data has been loaded")
	return data
end
--removes player--------------------------------
dataMod.removeSessionData = function(player)
	local key = "Player_" .. player.UserId
	sessionData[key] = nil

end

Players.PlayerRemoving:Connect (function(player)
	dataMod.save(player)
	dataMod.removeSessionData(player)

end)

return dataMod


Which line is this error coming from?

1 Like

ServerScriptService.ServerHandle.Data:107: attempt to index nil with ‘UserId’ - Server - Data:107

sorry ill update post

1 Like

Where is dataMod.set being called in the traceback? Your module’s implementation looks fine, so it may be the calling environment making the mistake.

As a simple test:

dataMod.set = function(player, stat, value)
    assert(typeof(player) == "Instance", 
        "player argument is not an instance")

    assert(player.ClassName == "Player",
        "player argument is not of class Player")

    local key = "Player_" .. player.UserId
    sessionData [key] [stat] = value
    player.leaderstats[stat].Value = value
end

ok i ran that and got …
107: player argument is not an instance

1 Like

So whatever function is calling dataMod.set is not providing a Player, and is instead providing nil (like the error says), or nothing at all.

You should be able to look into the traceback in output to figure out where some other script is passing in nil instead of a Player instance.

1 Like

Line 4. You’re attempting to get a player that isn’t in your game.

2 Likes

ok ill see what i find lets hope i can sort it thanks for the help you saved me about 500 years :smiley:

2 Likes

In dataMod.setupData,

Delete the duplicate call to dataMod.set.

1 Like