I have a script which is inside of a part, when the part is stepped on I want a UI to appear and I only want it to disappear when the player steps off the part.
I was struggling to find a way to detect when the player steps off a part as I was met by many mistakes and errors so I sticked to detecting when they are a certain amount of studs away, the UI goes invisible.
The script I’m currently using makes the UI appear shortly after stepping on it instead of checking the studs and only going invisible when its a certain amount of studs away. Any help on this would be appreciated or assistance on how to adjust the script so it detects when the player steps off the part then the UI goes invisible instead of using studs?
local part = script.Parent
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local effectsFrame = player.PlayerGui.Frames.EffectsFrame
local debounce = false -- Initialize debounce
local distanceCheckInterval = 0.5
local debounceDuration = 1
local function toggleVisibility(visibility)
if debounce or effectsFrame.Visible == visibility then
return
end
effectsFrame.Visible = visibility
debounce = true
wait(debounceDuration)
debounce = false
end
toggleVisibility(true)
while true do
wait(distanceCheckInterval)
if not part:IsDescendantOf(player.Character) or player:DistanceFromCharacter(part.Position) > 20 then
toggleVisibility(false)
break
end
end
end
end)