[BUG] - UserId index nil

Script :

local function autoSaves(plr)
local key = plr.UserId
print(key)
end

while wait(6) do
autoSaves()
end

Error code : ServerScriptService.Script:3: attempt to index nil with 'UserId’

Can somebody help me?

1 Like

You call the autoSaves function without the player parameter, in your function here:

local function autoSaves(plr) you expect the player parameter to be passed.

If your player isn’t nil, then this should fix it.

while wait(6) do
  autoSaves(plr)
end

The problem still exist with the same error.

Then that probably means your player is nil, have you tried putting this in the player added event with a plr parameter in the event? (assuming this is the whole script)

1 Like

Alright i fix it Adding Player Added

game.Players.PlayerAdded:Connect(function(plr)
	while wait(6) do
		autoSaves(plr)
	end
end)