Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I get the player that clicks a SurfaceGUI?

Asked by 7 years ago
local function OnClicked()
    game.Workspace.Particle:Clone().Parent = click.Parent.HumanoidRootPart
end

script.Parent.MouseButton1Click:connect(OnClicked)

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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)
Ad
Log in to vote
-2
Answered by 7 years ago

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.

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. Goulstem 8144 — 7y
0
isn't MouseButton1Down used for buttons? BlackOrange3343 2676 — 7y
0
He's is trying to click a surface GUI for something to function BlackOrange3343 2676 — 7y
0
You may be thinking of the `MouseClick` event - which does return the player that clicked. But that is for ClickDetectors, not GuiButtonObjects. Goulstem 8144 — 7y

Answer this question