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

How do I fix my 'object detecting' script?

So basically, I made a script for my AI car, and I want it to stop when something was in front of it, and that's what I made. But what make's me confused is that why does it keep driving when something is in front of it instead of stopping?

Script:

local leftDrive = script.Parent.Chassis.WheelRL.WheelMount.LeftDrive
local rightDrive = script.Parent.Chassis.WheelRR.WheelMount.RightDrive
local stopLight1 = script.Parent.LightParts.TailLightPart1
local stopLight2 = script.Parent.LightParts.TailLightPart2

script.Parent.StopDetector.Touched:Connect(function(hitstop)
	if hitstop.Name == "BodyFrame" then
		print("Stopping Car")
		leftDrive.AngularVelocity = 0
		rightDrive.AngularVelocity = 0
		stopLight1.Material = Enum.Material.Neon
		stopLight2.Material = Enum.Material.Neon
	else
		print("Driving Car")
		leftDrive.AngularVelocity = 80
		rightDrive.AngularVelocity = -80
		stopLight1.Material = Enum.Material.SmoothPlastic
		stopLight2.Material = Enum.Material.SmoothPlastic
	end
end)

1 answer

✓ Accepted answer

Maybe Try This:

local leftDrive = script.Parent.Chassis.WheelRL.WheelMount.LeftDrive
local rightDrive = script.Parent.Chassis.WheelRR.WheelMount.RightDrive
local stopLight1 = script.Parent.LightParts.TailLightPart1
local stopLight2 = script.Parent.LightParts.TailLightPart2

script.Parent.StopDetector.Touched:Connect(function(hitstop)
    print(hitstop.Name) -- You May Remove This If It Works Otherwise Use It For Testing
    print(hitstop.Parent.Name) -- You May Remove This If It Works Otherwise Use It For Testing
    if hitstop.Name == "BodyFrame" then
        print("Stopping Car")
        leftDrive.AngularVelocity = 0
        rightDrive.AngularVelocity = 0
        stopLight1.Material = Enum.Material.Neon
        stopLight2.Material = Enum.Material.Neon
        stopLight1.Anchored = true
        stopLight2.Anchored = true
    elseif hitstop.Parent.Name == "BodyFrame" then
        print("Stopping Car")
        leftDrive.AngularVelocity = 0
        rightDrive.AngularVelocity = 0
        stopLight1.Material = Enum.Material.Neon
        stopLight2.Material = Enum.Material.Neon
        stopLight1.Anchored = true
        stopLight2.Anchored = true
    else
        print("Driving Car")
        leftDrive.AngularVelocity = 80
        rightDrive.AngularVelocity = -80
        stopLight1.Material = Enum.Material.SmoothPlastic
        stopLight2.Material = Enum.Material.SmoothPlastic
        stopLight1.Anchored = false
        stopLight2.Anchored = false
    end
end)

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