Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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.

01local player= game.Players.LocalPlayer
02 
03  local function isowner()
04    if player.UserID==263105732 then
05        player.Position= game.Workspace.spawn
06    else
07        local fire= Instance.new("Fire",player)
08        for i= 0,100,1 do
09            player.Character.Humanoid.Health= player.Character.Humanoid.Health - 1
10        end
11    end
12end
13 
14game.Players.PlayerAdded:Connect(isowner)

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

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) speedyfox66 237 — 5y
0
This isn't a localscript, but I might as well try putting it in there. I will put your answer into consideration too. Cyroxite 46 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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:

01local function isowner(player)-- this is the player that joined.
02    if player.UserID==263105732 then
03        player.Position= game.Workspace.spawn
04    else
05        local fire= Instance.new("Fire",player)
06        for i= 0,100,1 do
07            player.Character.Humanoid.Health= player.Character.Humanoid.Health - 1
08        end
09    end
10end
11 
12game.Players.PlayerAdded:Connect(isowner)
Ad

Answer this question