How to check if the player's character is still touching the part

i want to achieve this:

checking if the player is still touching the part

1 Like

local Touching = false
part.Touched:Connect(function()
Touching = true
end)
part.TouchEnded:Connect(function()
Touching = false
end)

This’ll work for a local script. If this is a server sided script then you can make a table which has each user’s ID as the index, and the value equal to false. Set it equal to true upon touching the part, then set it to false upon the touch ending.

1 Like

the problem with Touched and TouchEnded is that it really wierd, while you still touching the part, the touchEnded also fire sometime, it not really consistent.

1 Like

Then iterate through a player’s parts with a for i, v in pairs(character:GetChildren()) and for each value, use if table.find(part:GetTouchingParts(), v). If the table.find returns true, then the player is touching the part.

I’d personally use .Magnitude with RunService rather than touched events, as it’s more stable and you don’t have to worry about that occurring, a debounce would be useful for these scenarios, but for my code it’s not really necessary. An example would be:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local rs = game:GetService("RunService")
rs.RenderStepped:Connect(function()
	local toCompare = (char:WaitForChild("HumanoidRootPart").Position - game.Workspace.YOUR_PART.Position).Magnitude
	if toCompare <= 3 then
		plr.PlayerGui.UI_STUFF.Visible = true
	else
		plr.PlayerGui.UI_STUFF.Visible = false
	end
end)
-- localscript in StarterPack
2 Likes

That’s when your part’s size doesn’t matter, what if the part’s way too long? It’s gonna compare with the position of the part which will be in center of size.