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)