How to detect when a player is touching and touching a part

  1. What do you want to achieve? I want to make a GUI frame show up when the player touches a part but I also want the GUI frame to disappear when the player isn’t touching the part.

  2. What is the issue? When the player touches the part the GUI frame shows up but then it just disasters when the player is still touching the part.

  3. What solutions have you tried so far? I looked on this devforum to find other people who had the same problem and had a solution.

Does anyone have any ideas what going on? I checked to see if there anything wrong with the GUI side to see if there anything making the frame disappear but there nothing there.

Garden.Hitbox.Touched:Connect(function(Other_Part) – This is part of the code

		if Other_Part.Parent:FindFirstChild("Humanoid") and Other_Part.Parent.Name == Plr.Name then
			
			Garden_Events.Garden_Menu:FireClient(Plr, true) -- Tells the client side to make the GUI Frame visible
			
		end
		
	end)
	
	Garden.Hitbox.TouchEnded:Connect(function(Other_Part)
		
		if Other_Part.Parent:FindFirstChild("Humanoid") and Other_Part.Parent.Name == Plr.Name and Other_Part then
			
			Garden_Events.Garden_Menu:FireClient(Plr, false) -- Tells the client side to make the GUI frame to disappear
			
		end

	end)
3 Likes

Maybe use a “While” statement instead of “If”.

Like, “While player touching, do XYZ. Else, do ZYX.”

The “While” statement should work better.

1 Like
local part = workspace.Part -- or path to part

local limbs = table.create(game.Players.MaxPlayers)

part.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		-- limb of player
		
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if limbs[player] then
			table.insert(limbs[player], hit)
		else
			limbs[player] = {
				hit,
			}
			
			-- do stuff for first limb touched
		end
	end
end)

part.TouchEnded:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		-- limb of player
		
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if #limbs[player] == 1 then
			limbs[player] = nil
			
			-- do stuff for last limb untouched
		else
			table.remove(limbs[player], table.find(limbs[player], hit))
		end
	end
end)
2 Likes

I did it like this:

Hitbox

local event = game.ReplicatedStorage:WaitForChild("GuiEvent")
local hitbox = script.Parent

hitbox.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if player then
			event:FireClient(player, true)
		end
	end
end)

part.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		if player then
			event:FireClient(player, false)
		end
	end
end)

Local Script (Put in StarterGui)

local Frame = script.Parent.Frame
local event = game.ReplicatedStorage:WaitForChild("GuiEvent")

event.OnClientEvent:Connect(function(vis)
	Frame.Visible = vis
end)

It worked perfectly fine.

2 Likes

Thats what I did before looking for help.

3 Likes

That’s weird can i see your hitbox?

2 Likes

1 Like

Okay this worked but how could I get it to disappear?

1 Like

Try this code it works @Unofficial_Giftbox

1 Like

I have a question. Why are you check if Other_Part.Parent.Name == Plr.Name?
I don’t think this is nesscesary

1 Like

What do you mean by limb of the player. Like check if it has a humanoid?

1 Like

Just like my script only Humanoid is enough. And that is might be the one causes the gui not dissappearing

1 Like

You can just forget that. Thats to check to make sure that’s the right player to get access to garden menu.

Your gui appears and disappears as any limb touches the part which can give unexpected results, that code makes it so when the first limb touches you can run code and when the last limb untouches you can clean it up.

Why not just make it detect Only the Humanoid Root Part, instead of ANY limb. That might simplify it.

Either or works

The moment the part is touched the GUI shows up but then instantly dissappers

My script work too but it might have some problem but it work
https://streamable.com/7zibfn

Is this what is occuring to you when you’re trying my code or are you talking about your last code? Could you show the updated code if you’re talking about mine.

Maybe make it wait to run the Disappearing script till the part is untouched.