I have a brick that when touched a Gui will pop up on your screen. The only problem is, I can't figure out how to detect when the player is not on the brick, and have the script then destroy the Gui. I have tried script.destroy() but still nothing. Can someone please help me out with this?
local Gui = game.Lighting.World1 --Replace this with the location of your GUI function GiveGui(Player) if Player.PlayerGui:FindFirstChild(Gui.Name)~=nil then return end Gui:Clone().Parent=Player.PlayerGui end script.Parent.Touched:connect(function(hit) local Player=game.Players:GetPlayerFromCharacter(hit.Parent) if Player==nil then return end GiveGui(Player) end)
I see you know the Touched
event, but did you know that there is a TouchEnded
event? It fires whenever a part stops touching another part.
Just check to make sure that this script is in a regular Script, as opposed to a LocalScript or a ModuleScript.
Check this out:
local Gui = game.Lighting.World1 function GiveGui(Player) if not Player.PlayerGui:FindFirstChild(Gui.Name) then Gui:Clone().Parent=Player.PlayerGui end end function RemoveGui(Player) --A function to remove the GUI. for i,v in pairs(Player.PlayerGui:GetChildren()) do if v.Name == Gui.Name then v:Destroy() end end end script.Parent.Touched:connect(function(hit) local Player=game.Players:GetPlayerFromCharacter(hit.Parent) if Player then GiveGui(Player) end end) script.Parent.TouchEnded:connect(function(hit) --This fires when the part stops touching the Brick. local Player=game.Players:GetPlayerFromCharacter(hit.Parent) if Player then RemoveGui(Player) end end)
:)