How to make this GUI disappear when you walk away from a part?

Hi there!

I already have the base code for when the script makes the GUI appear when it touches a part, but I need help on removing it when you walk away. I am aware of the OnTouchEnded command, but I don’t know how to initiate it.
Things I’ve already tried:

1): I’ve tried copy and pasting my code and making it to where it’ll wait a couple of seconds for it go away with OnTouchEnded. That isn’t accomplishing what I need for my game, as it’ll cause the game to glitch out.
2): I’ve also tried copy and pasting again, but this time only making it to where it = false, so I’m really having have a hard time.

It’s in the StarterGui and not in the workspace or within the part. I just need help with this. Thank you!
The base code:

local part = game.Workspace.Part
part.Touched:Connect(function()
	script.Parent.ScreenGui.MyGUI.Visible = true
end)

If anyone could tell me on how to make the GUI disappear, please show it with an edited version of my script and explain what I could’ve done to fix it. I learn more with a hands-on experience step by step with help than just someone telling me. Disabilities do that to you. :frowning:

2 Likes

well first you would want to add in an if statement to check what actually touched it then inbetween the brackets at function add in a name like hit or part as that is the part that touched it

1 Like

You could use RunService.Stepped to check if the player is at least x studs away from the part

local connection
local RunService = game:GetService("RunService")
local character = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").LocalPlayer.CharacterAdded:Wait()

local part = game.Workspace.Part
part.Touched:Connect(function()
    connection = RunService.Stepped:Connect(function()
        local hrp = character:FindFirstChild("HumanoidRootPart")
        if (hrp.Position - part.Position).Magnitude > 15 then
            script.Parent.ScreenGui.MyGUI.Visible = false
            connection:Disconnect() -- Prevents it from running when the gui already disappeared
        end
    end
	script.Parent.ScreenGui.MyGUI.Visible = true
end)

2 Likes

Hi, there’s an error in your code. It’s not exactly working and I can’t work with this code if there’s an error that I’m not able to fix.
image

I thought it was .RenderStepped

1 Like

Try change .Stepped to .RenderStepped

Yeah, still not working unfortunately.

connection only works on heartbeat btw so instead do RunService.HeartBeat

Yeah, still not working. I just only wanted a revised version of my script, explaining what I could’ve done. :frowning:

it would be more like

local connection
local Rs = game:GetService('RunService')

local part = game.Workspace.Part

part.Touched:Connect(function(hit)
         if hit.Parent:FindFirstChild('Humanoid') then
             local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
            Player.PlayerGui.ScreenGui.MyGUI.Visible = true
             connection = Rs.HeartBeat:Connect(function()
                 if Player:DistanceFromCharacter(part.Position) > 5 then
                          Player.PlayerGui.ScreenGui.MyGUI.Visible = false
                          connection:Disconnect()
                    end
             end)
       end
end)


1 Like

the 5 value is how far away in studs you can be i think

local part = game.Workspace.Part
part.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then --// checks if its a player
		script.Parent.ScreenGui.MyGUI.Visible = true
		
		repeat task.wait() until (part.Position - Hit.Parent.HumanoidRootPart.Position).magnitude >= 6
		
		script.Parent.ScreenGui.MyGUI.Visible = false
	end
end)
3 Likes

OH MY GOD, THANK YOU SO MUCH!!!

I finally know how to do it now, so thank you!!!

I forgot the ) on line 13