How would I make it so a GUI doesn't appear the second time after clicking a ClickDetector?

I’m currently almost finished with my lockdown system but when I press the lockdown button again (To end the lockdown) everything works but the LockdownGui will play because it was pressed…

Gui Code:

local button = game.Workspace.LockdownButton
local object = script.Parent
local clickdetector = button.ClickDetector


function onMouseClick(player)
	object.AnchorPoint = Vector2.new(0, 0)
	object.Position = UDim2.new(0.386, 0, -0.3, 0)
	wait(0.5)
	object:TweenPosition(UDim2.new(0.386, 0, 0, 0), 'Out', 'Quint', '4')
	end
	


clickdetector.MouseClick:connect(onMouseClick)

Lockdown Code:

local sound = Instance.new("Sound", game.Workspace)
local reverb = Instance.new("ReverbSoundEffect", game.Workspace.Sound)
reverb.WetLevel = "-13"
sound.SoundId = "rbxassetid://4621098419"

local LockdownButton = script.Parent
local clickDetector = LockdownButton.ClickDetector
local clicked = false
local lights = game.Workspace.Lights

function onMouseClick()
	if not clicked then 
		clicked = true 
		
		LockdownButton.BrickColor = BrickColor.Red()
		sound.SoundId = "rbxassetid://4621098419"
		sound.Playing = true
			wait(9)
			sound.Playing = false
			sound.SoundId = "rbxassetid://394634971"
			sound.Looped = true
			sound.Playing = true
		for k, light in pairs(lights:getChildren()) do
			light.Light.Color= Color3.fromRGB(255, 58, 58)
		end
	else
		clicked = false
		LockdownButton.BrickColor = BrickColor.Green()
		sound.Playing = false
		sound.SoundId = "rbxassetid://1526192493"
		sound.Playing = true
		wait(4)
		sound.Playing = false
		for k, light in pairs(lights:getChildren()) do
			light.Light.Color= Color3.fromRGB(255, 255, 255)
		end

	end
end

clickDetector.MouseClick:connect(onMouseClick)

you need put wait() after the event to the gui code

1 Like

Where exactly do I put wait, could you be more specific?

local button = game.Workspace.LockdownButton
local object = script.Parent
local clickdetector = button.ClickDetector


function onMouseClick(player)
wait() -- seconds set by you
	object.AnchorPoint = Vector2.new(0, 0)
	object.Position = UDim2.new(0.386, 0, -0.3, 0)
	wait(0.5)
	object:TweenPosition(UDim2.new(0.386, 0, 0, 0), 'Out', 'Quint', '4')
	end
	


clickdetector.MouseClick:connect(onMouseClick)
1 Like

I’d suggest that you use a remoteevent for it, so keep the server side click handler.
Fire the remote event when lockdown is started

On the client side when the remote event is fired, then show the GUI

1 Like

It will still show the GUI after the button is pressed to end it. (It just appears again)

I’m not really familiar with remote events and that type of stuff, little bit complex for someone like me.

They are not that bad!

Remote Functions and Events (roblox.com) has a load of examples. But the one you want here is the Server to Client, the examples on the link here might help.
A question - do you want the GUI to be shown for just the player who pressed the button. Or should it show to all players (or all players on a particular team maybe??)

1 Like

I’d want it to show for all players so everyone including prisoners and guard, etc can see it.

ok, pretty sure you would need to go with the remoteevents then, cos if you try to use the clickdetector like you do, it will only show up for the person who pressed the button.

Should not be too, hard is something like

  1. Add a RemoteEvent to ReplicatedStorage, call it something. Here I’ve called it “LockdownEvent”

in your server code add somewhere at the top

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LockdownEvent = ReplicatedStorage:WaitForChild("LockdownEvent")

In your click handler in the server code, after you’ve gone into lockdown call the event saying lockdown has started; so something like

LockdownButton.BrickColor = BrickColor.Red()
LockdownEvent:FireAllClients("start")

and I’m guessing you want to hide the gui when lockdown ends, so call the event here too

LockdownButton.BrickColor = BrickColor.Green()
LockdownEvent:FireAllClients("end")

You then need a local script to handle this, so put anywhere a local script can run (starterplayerscripts or wherever you prefer)

Has same stuff at top to access the event

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LockdownEvent = ReplicatedStorage:WaitForChild("LockdownEvent")

local function onLockdownEvent(action)
  if action == "start" then
    --code to show GUI here
  end

  if action == "end" then
    -- code to hide GUI here
  end
end

LockdownEvent.OnClientEvent:Connect(onLockdownEvent)

If I’ve not got the hide/show logic right (e.g. if the GUI automatically hides after a few seconds instead, or something like that) then it would need a bit of changing round, but hopefully gives a start.

1 Like

Allow the lockdown code to fire a RemoteEvent to one client(FireClient) or clients around(FireAllClients) whenever lockdown is initiated. The GUI code on the client should listen to this remote for it to display this GUI.

1 Like

Hmm… there seems to be an infinite yield interfering with the code for the Replicated Storage Event

image

It still seems to show after the event…

Game Link to see whats wrong, press the button once to start the lockdown then again to end it (When it ends you’ll see what I mean)

That normally means that there isn’t an event called LockdownEvent added to ReplicatedStorage

Could it be you’ve got the event added there but it’s got a different name maybe?


local button = game.Workspace.LockdownButton
local object = script.Parent
local clickdetector = button.ClickDetector
local db = false

function onMouseClick(player)
   if db == false then
        db=true
	    object.AnchorPoint = Vector2.new(0, 0)
	    object.Position = UDim2.new(0.386, 0, -0.3, 0)
	    wait(0.5)
	    object:TweenPosition(UDim2.new(0.386, 0, 0, 0), 'Out', 'Quint', '4')
    end
end


clickdetector.MouseClick:connect(onMouseClick)

GUI appears once with the debounce.

This will just make it so the it goes straight to the ending sequence, not the actual lockdown.

I tried this and the yield stopped but the GUI still appears at the end, let me edit my post to add the game link so you can see for yourself.

https://www.roblox.com/games/7051095483/Prison

If you want to check what I am talking about, press the button once to trigger the event then again to end it and you’ll see what I mean.