I have some code to make a gui pop up when a player steps on a certain brick, however when they move a new gui pops up how do I make it so it won’t pop up if one is already shown on the screen?
Script
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
game.ReplicatedStorage.RemoteEvent2:FireClient(plr)
end
end)
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
if StarterGui.ColorBlindTest.Enabled = false then
if StarterGui.ColorBlindLockScreenTest.Enabled = false then
game.ReplicatedStorage.RemoteEvent2:FireClient(plr)
end
end)
As everyone said above, add a check to see if there are any existing GUIs there.
This can be done by simply searching inside the player’s PlayerGui to see if there is the gui, and if there isn’t then it will fire the remote event;
EDIT; It seems your remote event enables the GUI instead of cloning a gui so make the check see if the GUI is enabled
Script that checks for GUI
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
if not plr:WaitForChild("PlayerGui"):FindFirstChild("GUINAME") then -- Checks if there's a gui named "GUINAME" in the player gui and if there isn't it will move to the next line
game.ReplicatedStorage.RemoteEvent2:FireClient(plr)
end
end
end)
Script that checks for GUI Visibility
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
if not plr:WaitForChild("PlayerGui"):FindFirstChild("GUINAME").Enabled == true then -- Checks if there's a gui named "GUINAME" in the player gui that is enabled and if there isn't it will move to the next line
game.ReplicatedStorage.RemoteEvent2:FireClient(plr)
end
end
end)
-- Your script
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
if StarterGui.ColorBlindTest.Enabled = false then
if StarterGui.ColorBlindLockScreenTest.Enabled = false then
game.ReplicatedStorage.RemoteEvent2:FireClient(plr)
end
end)
This is throwing an error because if statements need a double equals instead of one.
You need the ends to close the if statements.
Also it’s checking the startergui not the playergui.
-- Fixed script
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
if plr:WaitForChild("PlayerGui").ColorBlindTest.Enabled == false then
if plr:WaitForChild("PlayerGui").ColorBlindLockScreenTest.Enabled == false then
game.ReplicatedStorage.RemoteEvent2:FireClient(plr)
end
end
end
end)
So I have a part and when the player steps on it then a Gui shows up. But when they move around while the gui is still showing another gui shows up. The remote event is in the part so when a player touches the part it makes the gui show up.
It’s still not working and no errors are coming up, here’s what I have.
In the workspace under a brick I have a script that shows the following
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
if plr:WaitForChild(“PlayerGui”).ColorBlindTest.Enabled == false then
if plr:WaitForChild(“PlayerGui”).ColorBlindLockScreenTest.Enabled == false then
game.ReplicatedStorage.RemoteEvent2:FireClient(plr)
end
end
end
end)
In the StarterGui I have a screen gui named ColorBlindTest under that there’s a local script saying
Maybe add some print functions to see what’s not firing;
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
print(plr.Name.." touched the part B)")
if plr:WaitForChild(“PlayerGui”).ColorBlindTest.Enabled == false then
print("ColorBlindTest is not enabled")
if plr:WaitForChild(“PlayerGui”).ColorBlindLockScreenTest.Enabled == false then
print("ColorBlindLockScreenTest is not enabled")
game.ReplicatedStorage.RemoteEvent2:FireClient(plr)
print("Fired remote event")
end
end
end
end)
The words are printed in the outbox and they showed up twice because I moved around on the brick, so does that mean there’s something wrong with the code in the platform?
Can you clarify? What do you mean by “words” and “platform”?
If the platform you’re referencing is the brick then it’s fine; the print() function is being fired because everytime the brick is touched is fires the touch event. If you don’t want this you can add a debounce/cooldown to the touched event in the brick’s script.
local DebounceCooldown = 5
local Debounce = false
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
if Debounce == false then -- Checks if debounce is false
Debounce = true -- Sets debounce to true so that the rest of the code isn't being executed again
print(plr.Name.." touched the part B)")
if plr:WaitForChild(“PlayerGui”).ColorBlindTest.Enabled == false then
print("ColorBlindTest is not enabled")
if plr:WaitForChild(“PlayerGui”).ColorBlindLockScreenTest.Enabled == false then
print("ColorBlindLockScreenTest is not enabled")
game.ReplicatedStorage.RemoteEvent2:FireClient(plr)
print("Fired remote event")
end
end
wait(DebounceCooldown) -- Waits how many seconds the cooldown is
Debounce = false -- Sets debounce to false so that the code can be executed again
end
end)