GetTouchingPart wont work

I am making a script that detects if a player touches the red part and makes them lose a life but it wont work. I printed the touching parts names and it is only the parent of the character. There also will be a delay in someone touching the red part and the levels and modes will make different red parts move so touched part wont work since the red parts will move towards the player. I do not need help on moving the red parts, I just need help on GetTouchingPart. Here is the script provided:

local grid = game.Workspace:WaitForChild("Grid")
local boolean = script.Parent:WaitForChild("Started")
local debounceTable = {}

local function runLoop()
	local previousValue = boolean.Value
	while true do
		wait()
		if boolean.Value ~= previousValue then
			if boolean.Value then
				if script.Parent.CurrentLevel.Value == "Level 1" then
					if script.Parent.CurrentMode.Value == "Grid" then
						grid.Line15["15"].Color = Color3.fromRGB(0, 111, 190)
						grid.Line2["5"].Color = Color3.fromRGB(202, 0, 0)
					end
				end
			else
			end
			previousValue = boolean.Value
		end
	end
end

local function checkTouchingParts(player)
	local character = player.Character
	if not character then return end

	for _, part in ipairs(character:GetDescendants()) do
		if part:IsA("BasePart") then
			local touchingParts = part:GetTouchingParts()
			for _, touchedPart in ipairs(touchingParts) do
				local currentAncestor = touchedPart.Parent
				while currentAncestor do
					currentAncestor = currentAncestor.Parent
				end
				if touchedPart.Color == Color3.fromRGB(202, 0, 0) then
					if not debounceTable[player] or (os.clock() - debounceTable[player]) >= 2 then
						debounceTable[player] = os.clock()
						local lives = script.Parent:FindFirstChild("Lives")
						if lives and lives.Value > 0 then
							game.Workspace.Speaker1.LIfeLost:Play()
							lives.Value -= 1
						end
					end
					return
				end
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		spawn(function()
			while true do
				wait(0.1)
				checkTouchingParts(player)
			end
		end)
	end)
end)

spawn(runLoop)

Not really sure where you were trying to go with the “CurrentAncestor” loop, but that might be one of the issues.
Another issue could be that, for example, script.Parent:FindFirstChild("Lives") is nil.
Another thing: don’t use ipairs() or pairs(). It’s really not needed. In luau, you can simple write for i, v in tbl do end and it’ll work just fine.
wait() should be avoided too. Using task library’s task.wait() is preferred.

Not sure why you’re looping through ancestors – that part’s doing nothing and might break early exits. Also:

  • Color checks are cursed
    Lighting/material changes break them. Add a BoolValue named “Deadly” to red parts instead.

  • Stop checking every body part
    Just check the HumanoidRootPart:

local root = character:FindFirstChild("HumanoidRootPart")  
if root then  
   for _, touchedPart in root:GetTouchingParts() do  
       if touchedPart:FindFirstChild("Deadly") then  
           -- Your code  
       end  
   end  
end
  • Physics settings matter
    Moving parts need CanCollide=true, Anchored=false, and proper collision groups.

  • Debounce tweak
    Your 2-second cooldown is too long – use 0.2 for fast-moving hazards.

  • Script.Parent.Lives might be nil
    Replace script.Parent:FindFirstChild("Lives") with a direct reference if possible.

  • Never use wait()
    Replace with task.wait(0.1) or task.defer() for better performance.

  • Loop syntax
    for i,v in tbl do works fine – no need for ipairs().

If it still ghosts through parts, use Touched + GetTouchingParts() together. Let me see your revised script.

PS: If it looks like AI-Kind answer then know I did NOT make it with AI, I’m trying just to be as informative as possible. If you have any feedback on my answer then I am gonna try to improve it.

When I print, it keeps only printing the touch from the player character itself and not from the entire workspace

Found a sollution, the answer was using raycasting instead and making any body part touch the part it will print the name of it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.