Ad
Log in to vote
0

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

Asked by 4 days 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)

local replicatedstorage = game:GetService("ReplicatedStorage")
local givetool = replicatedstorage:WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer

givetool.OnServerEvent:connect(function()
    local weapon1 = replicatedstorage:WaitForChild("Thompson")
    weapon1:Clone().Parent = player.Backpack
    weapon1:Clone().Parent = player.StarterGear
end)

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

local replicatedstorage = game:GetService("ReplicatedStorage")
local givetool = replicatedstorage:WaitForChild("RemoteEvent")
local button = script.Parent:WaitForChild("Play")

button.MouseButton1Click:connect(function()
    givetool:FireServer()
end)

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 — 4d

Hi.

Looks like you're using an ad blocker.

That's fine...

No, it really is.

Just, I mean, we put a lot of work into this site, you know?

It would be really really appreciated if you would turn off your ad blocker for our website...

We tried really hard to make our ads as unobtrusive as possible.

If you really hate ads, would you consider a $2 donation via Patreon?

Here's the link.

We love you. We hope you love us too.

.

..

...

Now back to your regularly scheduled Scripting Helpers....

...

..

.

1 answer

Log in to vote
1
Answered by
Era_Gamer 119
4 days 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.

local replicatedstorage = game:GetService("ReplicatedStorage")
local givetool = replicatedstorage:WaitForChild("RemoteEvent")

givetool.OnServerEvent:connect(function(player)
    local weapon1 = replicatedstorage:WaitForChild("Thompson")
    weapon1:Clone().Parent = player.Backpack
    weapon1:Clone().Parent = player.StarterGear
end)

0
Thanks man, youre a life saver! GottaHaveAFunTime 25 — 4d
Ad

Answer this question



Ad