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.
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
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)
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)
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)