How do I detect when none of the players parts are touching a brick?

I’m trying to detect when none of a player’s character parts are touching a brick. Here is my the code I’ve written so far:

local rails = game.Workspace:WaitForChild("Rails")

game.Players.PlayerAdded:Connect(function(plr)
	local isTouchingRail = Instance.new("BoolValue")
	isTouchingRail.Parent = plr
	isTouchingRail.Name = "IsTouchingRail"
	isTouchingRail.Value = false
end)

for i, rail in pairs(rails:GetChildren()) do
	rail.Touched:Connect(function(part)
		if part.Parent:FindFirstChild("Humanoid") then
			local player = game.Players:GetPlayerFromCharacter(part.Parent)
			if player ~= nil then
				if player.IsTouchingRail.Value == false then
					player.IsTouchingRail.Value = true
				end
			end
		end
	end)
end

The problem is now detecting when none of the player’s parts are touching. Simply dectecting when something has stopped touching the part would not work because there’s a chance, for example, when a player’s leg stopped touching the part that another body part is still touching the part. What I want to achieve is detecting when no body parts are touching a brick. How do I achieve this?

you could maybe check if whatever the rails touched is nil or not

rail.Touched:Connect(function(hit)
   if hit == nil then
     --Do Stuff!
   end
end)

theres also a built in function i believe. Its called TouchEnded you could possibly use it to your advantage. heres the api reference

hope this helped :slightly_smiling_face:

Thank you! What I’m really trying to achieve is to detect when none of the players parts are touching. Maybe I’ll make a table that contains information about how many body parts are touching the part and then removing them. But I’m still looking for other solutions.

1 Like

Solved, I created a table that records the amount of body parts touching the rail. If it is equal to 0, it player.IsTouchingRail.Value will equal false.