Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How do I fix my 'object detecting' script?

Asked by 4 days ago

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

Log in to vote
0
Answered by 4 days ago

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)
Ad

Answer this question