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?