I’m trying to make a button(GuiSurface) that when clicked, knows the player that clicked.
I tried make this, but didn’t work
script.Parent.MouseButton1Click:Connect(function(plr))
I’m trying to make a button(GuiSurface) that when clicked, knows the player that clicked.
I tried make this, but didn’t work
script.Parent.MouseButton1Click:Connect(function(plr))
A SurfaceGui is a bit trickier than a regular Gui. I’ve always had trouble with them, so then I just parented them to the StarterGui and set the Adornee
property to the part I want it to be on. Then I use remote events to replicate what one player does to/on the Gui to all players.
It’s a good option, but the SurfaceGui needs to be parented to the Model I made to work fine.
How so? Please try saying what you are trying to do rather than ask how to do something that accomplishes what you want to do. It would save a lot of time and potentially expose errors or bad methods in your code.
so… The gui is inside a part(to work better) and has a button inside, and I want that when a player clicks the button, the script knows who clicked and(for example) prints the name of who clicked the button.
If I change the location of the gui, the script don’t work very well.
I recommend checking out these resourced to learn more about GUI’s. Also, make sure your using a Server Script instead of a local script.
If you want it to print the player then try doing this:
script.Parent.MouseButton1Click:Connect(function(plr)
print(plr)
end)
It prints “nil” or (when I put plr.Name) say that the “plr” don’t has a Name propertie
There is no parameters for the MouseButton1Click
event, why is which it’s resulting as a nil
value
Alternately, you could change the script in a LocalScript
& use RemoteEvents to print out the Player from the server side that way
--LocalScript in StarterPlayerScripts
local Player = game.Players.LocalPlayer
local SurfaceGui = workspace.RandomPart.SurfaceGui
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
SurfaceGui.TextButton.MouseButton1Click:Connect(function()
Event:FireServer()
print(Player.Name)
end)
--Server Script in ServerScriptService
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
Event.OnServerEvent:Connect(function(Player)
print(Player.Name)
end)