Script is thinking player is still touching part

I am making a script that detects if a player’s character is touching a brick. It works fine when the character is touching from the side of the part, but not on top. It sets a value to true if the player is still touching the brick, but once again, this only works when the player touches it from the side. The script creates a table that records how many body parts are touching the bricks and if it the amount of body parts is equal to 0 then it sets the value to false. Here is my code, pay closest attention to the for i, rail loop to the end because I’m pretty sure the issue is somewhere in there:

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

setmetatable(bodyPartTouching,
	{__index = function() return 0 end}
)

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
				bodyPartTouching[player.Name] += 1
				print(player.Name.." is touching rail with "..bodyPartTouching[player.Name].." body parts")
				if player.IsTouchingRail.Value == false then
					player.IsTouchingRail.Value = true
				end
			end
		end
	end)
	rail.TouchEnded:Connect(function(part)
		if part.Parent:FindFirstChild("Humanoid") then
			local player = game.Players:GetPlayerFromCharacter(part.Parent)
			if player ~= nil then
				bodyPartTouching[player.Name] -= 1
				if bodyPartTouching[player.Name] == 0 then
					player.IsTouchingRail.Value = false
					print("player has stopped touching part")
				end
			end
		end
	end)
end

It prints the amount of body parts touching the brick when I’m touching the brick. But if I stand one top, and walk off I don’t see a notice that says player has stopped touching part . Why is this happening?

Really what I need, is a script that can detect if a player is touching a brick at all or not. If someone could write a script that does that, it would be highly appreciated.


print every time touchended to make sure that’s working and print the amount of body parts in the table to make sure it doesn’t go below 0

1 Like

Tried it. It never went below 0. When I walk off the part, it says I still have one or two parts still touching.

maybe u can try looping :GetTouchingParts() from what i know touched is kinda buggy

Where should I put that in the loop of code? What block/chuck?

u could possibly use it in a while loop or a stepped loop and try without the touch and touchended since gettouchingparts returns a table of touching parts also just insert a empty part.touched:connect(function() end) bc gettouchingparts tend to bug and that seems to fix it

i gtg for now but I hope u get it to work

That is a good idea but one problem is part sorting. Who’s body part is who’s? Sorting may be inefficient. However, I will try your idea.

Edit: It definitely will be inefficient, especially with more players.

1 Like

you can just put them in a table im pretty sure