Need help to fix a GUI button script

  1. What do you want to achieve? Keep it simple and clear!
    I want this script to find the player name of the player who clicked the button of a gui in the workspace.
  2. What is the issue? Include screenshots / videos if possible!
    I cant find a working way to get the player that clicked the the button.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried to use parameter “player” with the function, but that did not work.

As stated earlier, this GUI button is inside of workspace and this script is a sever script

local module = require(game.ServerScriptService.StaffNotifyModule)
local button = script.Parent

function onClicked(player)
	local playername = player.Parent.Name
	module.Notify("Notif", playername.. " has clicked a staff button")
end

button.MouseButton1Click:Connect(onClicked)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

The MouseButton1Click function does not return the player, instead you can get the player by doing this.

local player = script:FindFirstAncestorWhichIsA('Player')

-- OR

local player = button:FindFirstAncestorWhichIsA('Player')

The player variable returns nil.

Where is the script located? In serverscriptservice or in the gui? If it’s in serverscript service, use the second method.

But if it’s in the gui, use the first method.

The script is located in the GUI button.

Oh, both methods should work then. Can you show me your updated script?

Here you go.

local module = require(game.ServerScriptService.StaffNotifyModule)

local button = script.Parent

function onClicked()

local player = script:FindFirstAncestorWhichIsA('Player')

local playername = player.Name

module.Notify("Notif", playername.. " has clicked a staff button")

end

button.MouseButton1Click:Connect(onClicked)

The script worked fine for me, the only thing I changed are the module parts.

--local module = require(game.ServerScriptService.StaffNotifyModule)
local button = script.Parent

function onClicked()
	local player = script:FindFirstAncestorWhichIsA('Player')
	local playername = player.Name

	--module.Notify("Notif", playername.. " has clicked a staff button")
	print(playername)
end

button.MouseButton1Click:Connect(onClicked)

image

Just to make sure, did you read the part where I said that the GUI button is located inside of the workspace?

Oh, it’s a surface gui? Try moving it into StarterGui.

I want it to stay in the surface gui.

Yes, I know. I meant move the surface gui into starter gui.

Edit : Also, you need to adornee the surface gui to the part.

But it will remove the button from the part, which I do not want it to do.

Nope, adornee doesn’t removes it.

Just set it’s adornee to the part. But if you really don’t want to parent it to starter gui. Try using remote events.

-- local script (game.StarterPlayer.StarterPlayerScripts)

local re = game.ReplicatedStorage.ButtonClick

workspace.Part.SurfaceUI.Button.MouseButton1Click:Connect(function()
    re:FireServer()
end)
-- server script (inside the button, or in server script service)
local Module = ...
local re = game.ReplicatedStorage.ButtonClick

re.OnServerEvent:Connect(function(Plr)
    Module.Notify("Notif", Plr.Name, "has clicked a staff button")
end)

Edit : Keep in mind that this is quite unsafe as an exploiter could fire this remote event very easily, though I have no idea on how I can make it more secure.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.