Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make a vehicle stop moving when the player jumps off of it?

Asked by
soutpansa 120
5 years ago

Is there any way to stop this boat from moving if a player jumps from the seat? Here is the script:


seat = script.Parent gyro = seat.BodyGyro pos = seat.BodyPosition vel = seat.BodyVelocity pos.position = seat.Position gyro.cframe = seat.CFrame speed = 0 ready = true wait() seat.Anchored = false function move(property) if property == "Steer" then if not ready then return end ready = false steer = seat.Steer while steer ~= 0 do steer = seat.Steer if steer == 1 then gyro.cframe = gyro.cframe * CFrame.fromEulerAnglesXYZ(0,-.1,0) elseif steer == -1 then gyro.cframe = gyro.cframe * CFrame.fromEulerAnglesXYZ(0,.1,0) end wait() vel.velocity = seat.CFrame.lookVector * speed end ready = true elseif property == "Throttle" then throttle = seat.Throttle while throttle ~= 0 do if throttle == 1 and speed < seat.MaxSpeed then speed = speed + 1 elseif throttle == -1 and speed > 0 then speed = speed - 1 end wait() vel.velocity = seat.CFrame.lookVector * speed end end end seat.Changed:connect(move)

I tried to make it detect when a player was touching it but I was never able to get it working. Does anyone have any ideas?

3 answers

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

When a player sits on a seat, a weld named "SeatWeld" is created inside of the seat. The Part0 is the seat and the Part1 is the HumanoidRootPart of their character. The weld is removed once the player stops sitting on the seat. What you can do is listen for the ChildRemoved event and check if the SeatWeld was removed.

seat.ChildRemoved:Connect(function(child)
    if child.Name == "SeatWeld" then
        -- Do stuff
    end
end)

Additionally, I suggest that you localise your variables. You are using global variables in the top level scope of the script, which makes no sense. To declare a variable local, you just type the keyword local and then the name. For example:

local variable = 2

The usage of global variables is bad practice, it makes code messy. Global variables also pollute the global environment as well as increase the risk for name collisions

And finally, BodyGyro.cframe, BodyPosition.position, BodyVelocity.velocity, CFrame.lookVector, and RBXScriptSignal:connect() are deprecated, use BodyGyro.CFrame, BodyPosition.Position, BodyVelocity.Velocity, CFrame.LookVector, and RBXScriptSignal:Connect() instead.


Hopefully this answered your question and if it did then don't forget to hit that accept button. If you have any more questions then feel free to leave them in the comments.
Ad
Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
5 years ago
Edited 5 years ago

After a second look at your code I believe the issue is with the while loops' conditions.. they aren't directly indexing the Property, they are referencing a predefined variable.

while seat.Steer ~= 0 do --line 20

and

while seat.Throttle ~= 0 do --line 33
Log in to vote
-1
Answered by 5 years ago

When answering, if your answer does not fully solve the question, it should be written as a comment to the question instead of as an answer.

Check the Occupant. Class/VehicleSeat.Occupant

In the Vehicle Seat, there is a property called "Occupant", continue checking until the player jumps off which changes the property to nil, which then you can then stop the vehicle from moving. {anchor it down, set the speed to zero, do whatever you need to do}

Answer this question