Ad
Log in to vote
0

It displays an error when I try to get, a player variable, Why?

Asked by 9 days ago

Here is the code:

script.Parent.MouseButton1Click:connect(function(player)
    print(player.Name)
end)

but it displays: workspace.CarSpawner.SpawnerButton.SurfaceGui.TextButton.Script:2: attempt to index local 'player' (a nil value) in the output

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....

...

..

.

2 answers

Log in to vote
1
Answered by 9 days ago

You can't locate the player like that. The easiest way is to put it in a local script and define player as game.Players.LocalPlayer.

Ad
Log in to vote
1
Answered by 9 days ago

I assume that you have a Server Script inside a button but when working with GUI's its best to use a local script so you can easily access the player

local button = script.Parent
local player = game.Players.LocalPlayer
button.MouseButton1Click:Connect(function()
    print(player.Name)
end)

Hopefully this helped you Please submit this answer if it did!

0
Lol exactly what I said 3 minutes before you VeryRaven 63 — 9d
0
I tried to use a localscript but it did nothing iiLevelMaker 56 — 9d
0
Is the local script parent the button?? saSlol2436 147 — 9d
0
Is the local script parent the button?? saSlol2436 147 — 9d
View all comments (2 more)
0
Is the local script parent the button?? saSlol2436 147 — 9d
0
Is the local script parent the button?? saSlol2436 147 — 9d

Answer this question



Ad