Gui doesn't disable when touching a part

:wave:Hey Scripters!
I’m currently trying to program a brick so that whenever you touch it, a GUI disappears. Here is my code for the brick:

local players = game:GetService("Players")
local leave = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")

function onTouch(part)
	leave.Enabled = false
end

script.Parent.Touched:Connect(onTouch)

If you were wondering, the GUI is generated every time you select a mission and is disabled every time you click ‘Go back to the lobby’

Hopefully you can help me! I’ve been looking on the DevForum for about 4 hours now trying to find something that can help me :slight_smile:

4 Likes

So what’s supposed to be disabled?

The Gui that states ‘Go to Lobby’
in the video you can see it in the bottom right

Is that code in a localscript that’s inside the part?

No its a serverscript
Yes its in the part

If it helps I can send the script that actually makes the GUI appear when you select a mission


You cannot use LocalPlayer inside a ServerScript. Instead, you should listen for the .Touched event and then check if the toucher is a player and find the gui from the player.

local PS = game:GetService("Players")

local part: BasePart = script.Parent

part.Touched:Connect(function(toucher: BasePart)
	local player = PS:GetPlayerFromCharacter(toucher:FindFirstAncestorWhichIsA("Model"))
	
	if player then
		local gui: ScreenGui = player.PlayerGui:WaitForChild("ScreenGui")
		
		if gui then
			gui.Enabled = false
		end
	end
end)
4 Likes

Hey, thanks for the reply!
I tried out your code and it still doesn’t work
The part now has a Localscript instead of a server script
At the very end when the gui is being disabled I put a print command however nothing shows up in the output so something isn’t running
However, there aren’t any errors in the output either

Don’t use a local script. Copy my code and paste in in a server script.

1 Like

Oh ok thanks I’ll try that
Give me a minute

Ok I tried out your code
At the very end it does print out what I said so that means the script is running
However the GUI isn’t being disabled
Really confused on this
When I go into explorer and manually disable it in properties it does disable so that means there is no other script counter-acting the disable script

1 Like

You may need to move the character a little bit when you get teleported. Touch events don’t always activate correctly

1 Like

Yup you’re right it works now
Thanks!

1 Like

Is there a way to make the same thing but on part touch disable another script that is in the workspace

Yes it’s possible to disable a script by just touching a part and it’s quite simple.

local part = script.Parent
local disabledScript = game.Workspace.Script

part.Touched:Connect(function(player)
	if player then
		disabledScript.Disabled = true
	end
end)

This just uses a Touched event to see if something touches the part and then it disables a script.

1 Like