Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

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

2 comments

0

this did not work, the part still stayed green :/

0

Updated my post slightly. What does printing out humanoid output in console?

This discussion is preserved as it was. New answers, comments, and votes are closed.