I want to make my car's engine script to respond to events by the seat. Not a while loop. How do i do that?
local car = script.Parent.Parent local topspeed = car.VehicleSeat.Configuration.TopSpeedLevel.Value local handling = car.VehicleSeat.Configuration.HandlingLevel.Value local acceleration = car.VehicleSeat.Configuration.Acceleration.Value local speed = 150 function Steer() if script.Parent.Steer == 1 then car.Axle.SteerHinge.TargetAngle = 5 car.Axle.SteerHinge.ServoMaxTorque = car.Axle.SteerHinge.ServoMaxTorque + (0.1*handling) * car.Axle.SteerHinge.ServoMaxTorque script.Parent.Parent.Rod.HingeConstraint.TargetAngle = -20 end if script.Parent.Steer == -1 then car.Axle.SteerHinge.TargetAngle = -5 car.Axle.SteerHinge.ServoMaxTorque = car.Axle.SteerHinge.ServoMaxTorque + (0.1*handling) * car.Axle.SteerHinge.ServoMaxTorque script.Parent.Parent.Rod.HingeConstraint.TargetAngle = 20 end if script.Parent.Steer == 0 then car.Axle.SteerHinge.TargetAngle = 0 script.Parent.Parent.Rod.HingeConstraint.TargetAngle = 0 car.Axle.SteerHinge.ServoMaxTorque = 2*(car.Axle.SteerHinge.ServoMaxTorque + (0.1*handling) * car.Axle.SteerHinge.ServoMaxTorque) end end function OnActivated() if script.Parent.Throttle == 1 then car.Transmission.LeftDrive.MotorMaxTorque = car.Transmission.LeftDrive.MotorMaxTorque + (0.1*acceleration) * car.Transmission.LeftDrive.MotorMaxTorque car.Transmission.RightDrive.MotorMaxTorque = car.Transmission.RightDrive.MotorMaxTorque + (0.1*acceleration) * car.Transmission.RightDrive.MotorMaxTorque car.Transmission.LeftDrive.AngularVelocity = speed * (1.1*topspeed) + speed car.Transmission.RightDrive.AngularVelocity = -(speed * (1.1*topspeed) + speed) script.Parent.Parent.Throttle.Rot.HingeConstraint.TargetAngle = 20 end if script.Parent.Throttle == -1 then car.Transmission.LeftDrive.MotorMaxTorque = car.Transmission.LeftDrive.MotorMaxTorque + (0.1*acceleration) * car.Transmission.LeftDrive.MotorMaxTorque car.Transmission.RightDrive.MotorMaxTorque = car.Transmission.RightDrive.MotorMaxTorque + (0.1*acceleration) * car.Transmission.RightDrive.MotorMaxTorque car.Transmission.LeftDrive.AngularVelocity = -(speed * (1.1*topspeed) + speed ) car.Transmission.RightDrive.AngularVelocity = speed * (1.1*topspeed) +speed script.Parent.Parent.Throttle.Rot.HingeConstraint.TargetAngle = -20 end if script.Parent.Throttle == 0 then car.Transmission.LeftDrive.MotorMaxTorque = car.Transmission.LeftDrive.MotorMaxTorque + (0.1*acceleration) * car.Transmission.LeftDrive.MotorMaxTorque car.Transmission.RightDrive.MotorMaxTorque = car.Transmission.RightDrive.MotorMaxTorque + (0.1*acceleration) * car.Transmission.RightDrive.MotorMaxTorque car.Transmission.LeftDrive.AngularVelocity = 0 car.Transmission.RightDrive.AngularVelocity = 0 script.Parent.Parent.Throttle.Rot.HingeConstraint.TargetAngle = 0 end end local object = script.Parent -- vehicle seat object:GetPropertyChangedSignal("Throttle"):Connect(function(OnActivated) end) object:GetPropertyChangedSignal("Steer"):Connect(function(Steer) end)
When i try to turn steer or throttle then it won't do anything. I think the problem is with the last 4 lines of code.
You're not connecting the functions themselves. You're merely giving the functions a parameter; a functions input variables.
object:GetPropertyChangedSignal("Throttle"):Connect(onActivated) object:GetPropertyChangedSignal("Steer"):Connect(Steer)
This connects the functions.
Pretty much what you were doing is connecting up a function literal:
object:GetPropertyChangedSignal("Throttle"):Connect(function(onActivated) end)
onActivated
is merely a parameter to the function. You're not connecting the onActivated
function itself. You made the same mistake for the steer.
object:GetPropertyChangedSignal("Throttle"):Connect(onActivated)
This connects the onActivated
function itself.
Hope I cleared it up better.