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

How do I get the player that clicks a SurfaceGUI?

local function OnClicked()
	game.Workspace.Particle:Clone().Parent = click.Parent.HumanoidRootPart
end

script.Parent.MouseButton1Click:connect(OnClicked)

2 answers

✓ Accepted answer

##Problem

SurfaceGuis can be a bit tricky - and i'll explain why.

You're probably used to parenting the SurfaceGui to your object, and working from there. This is a common misconception because Gui objects are meant to run on the client.


##Solution

In order to have you gui on the client you need to keep it in StarterGui, and set the Adornee property. This property is basically where the gui is going to show up.

And now that the gui is on the client, you can simply index game.Players.LocalPlayer from a LocalScript to access the player.

local click = game.Players.LocalPlayer --This is the client
local adorn = workspace.Part --This is the object the gui will be on

script.Parent.Parent.Adornee = adorn;

local function OnClicked()
	game.Workspace.Particle:Clone().Parent = click.Character.HumanoidRootPart
end

script.Parent.MouseButton1Click:connect(OnClicked)

First, you can't just click a Surface Gui, Add a button into the surface Gui.

after all of that make sure the button is on the part visible so you can click it for later on.

Now your code will be edited;

local button = script.Parent

button.MouseButton1Down:connect(function(Clicked)
	local clone = game.Workspace:WaitForChild("Particle"):Clone()
	clone.Parent = Clicked.Parent:WaitForChild("HumanoidRootPart")
end)

Put this code in a local script in the button, button should be in Surface Gui

If helped, please Upvote and accept answer, if you have any other questions please comment down below. If doesn't work explain errors and so on.

4 comments

0

The MouseButton1Down event doesn't return the player that clicked. It returns the x and y coordinates of where on the ButtonObject was clicked.

0

isn't MouseButton1Down used for buttons?

0

He's is trying to click a surface GUI for something to function

0

You may be thinking of the MouseClick event - which does return the player that clicked. But that is for ClickDetectors, not GuiButtonObjects.

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