This is a car i scripted:
it works fine, but when you get out of the Car it keeps going. So I tried to make a script that sets the MaxSpeed of the car to 0 if the VehicleSeat property “Occupant” is set to nil. But in my script right now i have a OccupantChanged function that sets a bool value called “stop” to false, and in the Client Script, the script that makes the car go, it checks if stop is true and from there its magic. But its not. The script doesn’t do that, so for more information on my car, I have a car handling script that deals with players getting in and out the car. The other one deals with the driving. so here are my scripts and some visuals on my problem:
The Car Handling script
local car = script.Parent
local seat = car.Body.VehicleSeat
local Body = car.Body.Body
local cooldown = 0
local occupiedPlayer
local physicService = game:GetService("PhysicsService")
local defaultCollisonGroup = "Default"
local chrCollsiongroup = "Char"
local CarClient = script.CarClient
local function CoolDown(duration)
local cooldowntag = tick()
cooldown = cooldowntag
delay(duration,function()
if (cooldown == cooldowntag) then
cooldown = 0
end
end)
end
local function CharCollide(char, ans)
local group = (ans and defaultCollisonGroup or chrCollsiongroup)
for _,part in ipairs(char:GetDescendants())do
if part:IsA("BasePart")then
part.Massless = not ans
physicService:SetPartCollisionGroup(part,group)
end
end
end
local function PartTouched(part)
if (seat.Occupant or cooldown ~= 0) then return end
local character = part.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if (not player)then return end
local Humanoid = character:FindFirstChild("Humanoid")
if (not Humanoid)then return end
seat:Sit(Humanoid)
occupiedPlayer = player
CharCollide(character, false)
car.PrimaryPart:SetNetworkOwner(player)
OccupiedClientScript = CarClient:Clone()
OccupiedClientScript.Car.Value = car
OccupiedClientScript.Parent = player.Backpack
CoolDown(1)
end
local function Occupantchange()--IMPORTANT PART
if (seat.Occupant) then return end
if (not occupiedPlayer)then
seat.MaxSpeed = 0
else
seat.MaxSpeed = 60
end
if (occupiedPlayer == nil) then return end
if (occupiedPlayer.Character) then
CharCollide(occupiedPlayer.Character, true)
end
if (OccupiedClientScript == nil) then return end
if (OccupiedClientScript.Parent)then
OccupiedClientScript.Stop.Value = true--This sets the stop to true, and the car is suppose to stop when the occupants change
local client = OccupiedClientScript
delay(3,function()
client:Destroy()
end)
end
car.PrimaryPart:SetNetworkOwnershipAuto()
occupiedPlayer = nil
OccupiedClientScript = nil
CoolDown(3)
end
Body.Touched:Connect(PartTouched)
seat:GetPropertyChangedSignal("Occupant"):Connect(Occupantchange)
Car client script, this is where i actually stop the car.
local car = script:WaitForChild("Car").Value
local stop = script:WaitForChild("Stop")
local seat = car.Body.VehicleSeat
local heartbeat
local attFL = car.Chassis.Platform.AttachmentFL
local attFR = car.Chassis.Platform.AttachmentFR
local attBL = car.Chassis.Platform.AttachmentBL
local attBR = car.Chassis.Platform.AttachmentBR
local CylBL = car.Chassis.Platform.CylindricalBL
local CylBR = car.Chassis.Platform.CylindricalBR
local CylFL = car.Chassis.Platform.CylindricalFL
local CylFR = car.Chassis.Platform.CylindricalFR
local ms = 30
local steer = 0
local throttle = 0
local ms = 30
local function Update(dt)
--Steer;
local goal = -seat.SteerFloat * ms
steer = steer + (goal - steer) * math.min(dt * seat.TurnSpeed, 1)
local radians = math.rad(90 - math.abs(steer))
local h = -(attFR.WorldPosition - attBR.WorldPosition).Magnitude
local z = (attBR.WorldPosition - attBL.WorldPosition).Magnitude
local x = math.tan(radians) * h
local y = (x + z)
local OTA = 90 - math.deg(math.atan2(y, h))
if (steer > 0)then
attFL.Orientation = Vector3.new(0, OTA, 90)
attFR.Orientation = Vector3.new(0, steer, -90)
else
--r
OTA = -OTA
attFL.Orientation = Vector3.new(0,OTA,90)
attFR.Orientation = Vector3.new(0,steer,-90)
end
--Throttle;
local ThrottleGoal = seat.ThrottleFloat
throttle = throttle + (ThrottleGoal - throttle) * math.min(dt * seat.TurnSpeed, 1)
local Torque = seat.Torque
local speed = seat.MaxSpeed * throttle
CylBL.MotorMaxTorque = Torque
CylBR.MotorMaxTorque = Torque
CylFL.MotorMaxTorque = Torque
CylFR.MotorMaxTorque = Torque
CylBL.AngularVelocity = speed
CylBR.AngularVelocity = -speed
CylFL.AngularVelocity = speed
CylFR.AngularVelocity = -speed
end
local function Start()--Makes the car go
print("Start Client")
heartbeat = game:GetService("RunService").Heartbeat:Connect(Update)
end
local function Stop()
print("Stop Client")
script:Destroy()
heartbeat:Disconnect()
script:Destroy()
end
Start()
if (stop.Value) then Stop() return end--if its true, stop the script.
stop.Changed:Connect(function(shouldStop)
if (not shouldStop) then return end
Stop()
end)