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

ServerScriptService.Script:5: attempt to index upvalue 'player' (a nil value)?

I am running this script where it checks is a player has this user id so it will teleport them somewhere else. I run this script and the output says that the UserId property for a LocalPlayer doesn't exist.

 local player= game.Players.LocalPlayer

  local function isowner()
	if player.UserID==263105732 then
		player.Position= game.Workspace.spawn
	else
		local fire= Instance.new("Fire",player)
		for i= 0,100,1 do
			player.Character.Humanoid.Health= player.Character.Humanoid.Health - 1
		end
	end
end

game.Players.PlayerAdded:Connect(isowner)

Any reason why? I am positive that I did everything correct.

2 comments

0

is this a localscript? if not, you'll have to get the identity of the player simply by checking when they join and getting that player (as a "local" or variable)

0

This isn't a localscript, but I might as well try putting it in there. I will put your answer into consideration too.

1 answer

✓ Accepted answer

The only logical explanation that I can think of is that you were using a regular script. Meaning that it executes server sided code. However Service Players's Property LocalPlayer can only have a value if it's referenced from the client side (by using a local script). So since LocalPlayer is client sided, a regular script will substitute it with nil.

To fix your problem, event PlayerAdded has a parameter where it's the player that joined. Use instead:

local function isowner(player)-- this is the player that joined.
    if player.UserID==263105732 then
        player.Position= game.Workspace.spawn
    else
        local fire= Instance.new("Fire",player)
        for i= 0,100,1 do
            player.Character.Humanoid.Health= player.Character.Humanoid.Health - 1
        end
    end
end

game.Players.PlayerAdded:Connect(isowner)

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