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

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

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

2 answers

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.

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!

6 comments

0

Lol exactly what I said 3 minutes before you

0

I tried to use a localscript but it did nothing

0

Is the local script parent the button??

0

Is the local script parent the button??

0

Is the local script parent the button??

0

Is the local script parent the button??

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