Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how do you detect if a part is not touched by a humanoid ?

Asked by 8 years ago

this Script detects when the humanoid touches it so that it will turn Lime green and when the humanoid is not touching it it turns white, but it just stays green, why is that?

local part = script.Parent

part.BrickColor = BrickColor.new("Institutional white")

local function handleTouch(otherpart)
    local character = otherpart.Parent
    local humanoid = character:FindFirstChild("Humanoid")

    if humanoid then
        part.BrickColor = BrickColor.new("Lime green")
        if not humanoid then
            part.BrickColor = BrickColor.new("Institutional white")
        end
    end

end

part.Touched:connect(handleTouch)

AND THERE IS NO ERRORS

2 answers

Log in to vote
0
Answered by 8 years ago

The reason this doesn't work is because you are only updating when a player does touch the part. When a player stops touching a part, the "Touched" event is not fired. To get around this, use the TouchEnded event.

This script will work inside of a BasePart

local Platform = script.Parent

local function CheckForHumanoidParts(Part)
    local PartsTouching = Platform:GetTouchingParts()
    local HumanoidIsTouching = false
    for _,BasePart in pairs(PartsTouching) do
        if BasePart.Parent:FindFirstChild("Humanoid") then
            HumanoidIsTouching = true
            break
        end
    end
    Platform.BrickColor = HumanoidIsTouching and BrickColor.new("Lime green") or BrickColor.new("Institutional white")
end

Platform.Touched:connect(CheckForHumanoidParts)
Platform.TouchEnded:connect(CheckForHumanoidParts)
Ad
Log in to vote
1
Answered by 8 years ago

The issue is with the nesting of your if statements. Lines 09-14 are contained underneath one if statement scope, and 11-13 are contained within that scope as a seperate scope, but due to the conditions of the first if statement, the second will never fire.

Lines 09-14 should be

    if humanoid then
        part.BrickColor = BrickColor.new("Lime green")
    else
            part.BrickColor = BrickColor.new("Institutional white")
    end
0
this did not work, the part still stayed green :/ HyperSpeed05 40 — 8y
0
Updated my post slightly. What does printing out humanoid output in console? RightLegRed 15 — 8y

Answer this question