Opening a Gui by clicking a player

So I have been struggling with scripting and making a GUI opening by clicking a player.
I planned that the GUI could show the badges of a player that got but sadly it didn’t make the final cut.
If someone can help me with this project I want to make it! I would be honored.

-- LocalScript in StarterCharacterScripts: PlayerClickedScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Create a RemoteEvent to communicate between client and server
local playerClickedEvent = Instance.new("RemoteEvent")
playerClickedEvent.Name = "PlayerClickedEvent"
playerClickedEvent.Parent = ReplicatedStorage

-- LocalScript in StarterCharacterScripts: PlayerClickedScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Create a RemoteEvent to communicate between client and server
local playerClickedEvent = Instance.new("RemoteEvent")
playerClickedEvent.Name = "PlayerClickedEvent"
playerClickedEvent.Parent = ReplicatedStorage

-- Define the name or identifier of the GUI to open
local guiName = "PlayerBadgeGui"  -- Replace "YourGUINameHere" with the actual name of your GUI

-- Function to handle ClickDetector mouse clicks
local function onCharacterClicked()
	-- Fire the RemoteEvent with the GUI name as an argument
	playerClickedEvent:FireServer(guiName)
end

-- Get the local player's character
local character = game.Players.LocalPlayer.Character

-- Check if the character exists and has a humanoid
if character then
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		-- Create a ClickDetector and connect it to the onCharacterClicked function
		local clickDetector = Instance.new("ClickDetector")
		clickDetector.Parent = humanoid
		clickDetector.MouseClick:Connect(onCharacterClicked)
	end
end

-- Listen for the PlayerClickedEvent from clients
playerClickedEvent.OnServerEvent:Connect(function(player, guiName)
	-- Get the GUI object by its name
	local gui = player.PlayerGui:FindFirstChild(guiName)
	if gui then
		-- Set the visibility of the GUI to true
		gui.Enabled = true
	else
		warn("GUI not found for player: " .. player.Name)
	end
end)

i’ve made a gui, used the clickdetector and the remoteEvent

1 Like

Maybe try asking this type of questions in the scripting support.

3 Likes

Alright! Thank you so much, I’m not very good at using the Devforum

Why is the server handling the click and GUI? The server can track the badges for each player and offer a completion list (and optionally update clients when another player gets a new badge). Each client can track clicks then open and populate the GUI with the target’s badges.

1 Like

Do you know how can I fix this with a short cut?

I don’t believe there’s a shortcut to making this work, and it’d be extra painful to carry around the logic that’s already in place. The way you set up the variable for the RemoteEvent is hinting to me that you might not be fully aware of how Instances replicate. You may want to brainstorm how this badge viewer system works before coding it.