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

Weirdest error of all time, "attempt to index upvalue 'player'(a nil value)?

Asked by 7 years ago

I have a GUI that gives a player a tool but it cant do it with FE on so I decided to use a RemoteEvent to trigger it and then give it to the player.

Here is the script(Located unter ServerScriptService)

1local replicatedstorage = game:GetService("ReplicatedStorage")
2local givetool = replicatedstorage:WaitForChild("RemoteEvent")
3local player = game.Players.LocalPlayer
4 
5givetool.OnServerEvent:connect(function()
6    local weapon1 = replicatedstorage:WaitForChild("Thompson")
7    weapon1:Clone().Parent = player.Backpack
8    weapon1:Clone().Parent = player.StarterGear
9end)

Here is the local script (Located under StarterGui.SurfaceGUI)

1local replicatedstorage = game:GetService("ReplicatedStorage")
2local givetool = replicatedstorage:WaitForChild("RemoteEvent")
3local button = script.Parent:WaitForChild("Play")
4 
5button.MouseButton1Click:connect(function()
6    givetool:FireServer()
7end)

The local script fires after pressing a button and then it triggers but it errors where it says "weapon1:Clone().Parent = player.Backpack. It says basically cant upvote player because its a nil value.

0
You can't use LocalPlayer in a server script. http://wiki.roblox.com/index.php?title=API:Class/Players/LocalPlayer biglulu 0 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago

You can't use game.Players.LocalPlayer in a server script as you did in the above examples. Instead, the player is automatically the first argument in .OnServerEvent function.

1local replicatedstorage = game:GetService("ReplicatedStorage")
2local givetool = replicatedstorage:WaitForChild("RemoteEvent")
3 
4givetool.OnServerEvent:connect(function(player)
5    local weapon1 = replicatedstorage:WaitForChild("Thompson")
6    weapon1:Clone().Parent = player.Backpack
7    weapon1:Clone().Parent = player.StarterGear
8end)
1
Thanks man, youre a life saver! GottaHaveAFunTime 218 — 7y
Ad

Answer this question