Hi all,
I am utterly lost on this subject. It should be an easy one, and I have searched for answers, but I cannot seem to find anything. My question is how I would go about making a vehicle rise and fall when pressing e or q (Think a helicopter). I don’t need any fancy tilting or anything, just the bare structure to get this working. I have tried getting it to work, but I cannot seem to get anywhere.
I am moving my vehicle with BodyForces, using a script under the vehicle seat. The Vehicle itself can move forward (w), Backward (s), left(a), and Right(d).
My current script (server script under the vehicle seat):
local Engine = script.Parent.Parent.Engine.BodyThrust --The Force making the vehicle move
local SForce = script.Parent.Parent.Steer.BodyAngularVelocity -- The force making the vehicle turn
local DSeat = script.Parent -- The driver seat
speed = 50000 -- The force of 'Engine'
SteerSpeed = 10000 -- The force of SForce
DSeat.Changed:Connect(function(p)
if p == "ThrottleFloat"then
Engine.Force = Vector3.new(0,0,speed * DSeat.ThrottleFloat)
end
if p == "SteerFloat"then
SForce.AngularVelocity = Vector3.new(0,-SteerSpeed*DSeat.SteerFloat,0)
end
end)
However, I cannot find out how to check when the player is sitting in the seat, and when they are sitting, tell when they press e or q. Once they press it, I want the vehicle to either rise in the air or lower in the air. This has really challenged me, I have no idea how to go about this. I have looked at this article and model, but I couldn’t seem to tweak it to serve my purposes (i don’t need the helicopter to tilt when moving or anything fancy, just rising or lowering) Link To Possible Solution I would love to clarify any questions you have, thank you! Any help is REALLY appreciated!
Basically you want to have a body force which is perpendicular to the upvector of the helicopter. Holding means positive up force negative means none. It is important you get your acceleration components right here or else you will climb up slowly and drop immediately. If we want a more realisitic (bug laggier) approach have it be where the force is equivalent to the centripital force of the blades cross product of its general velocity.
There are multiple ways to check if the player is sitting in the seat, the link you sent uses a server script to give the local script to the player which controls the plane, this piece of code is copied/inspired from the default Roblox jeep script free model:
Give local script to player
--Script
local driverseat = script.Parent
local main = script.Parent.Parent.Main
local plane = script.Parent.Parent
driverseat.Changed:connect(function(property)
if property == "Occupant" then
--Sitz ist belegt
if driverseat.Occupant then
--Get the sitting player
local player = game.Players:GetPlayerFromCharacter(driverseat.Occupant.Parent)
if player then
--Sets ownership to player
driverseat:SetNetworkOwner(player)
print(player.Name.." has Networkownership")
local localCarScript = script.playerScriptPlane:Clone()
localCarScript.Parent = player.PlayerGui
localCarScript.Obj.Value = plane
print("Localscript was inserted")
end
end
end
end)--Script
local driverseat = script.Parent
local main = script.Parent.Parent.Main
local plane = script.Parent.Parent
driverseat.Changed:connect(function(property)
if property == "Occupant" then
--Sitz ist belegt
if driverseat.Occupant then
--Get the sitting player
local player = game.Players:GetPlayerFromCharacter(driverseat.Occupant.Parent)
if player then
--Sets ownership to player
driverseat:SetNetworkOwner(player)
print(player.Name.." has Networkownership")
local localCarScript = script.playerScriptPlane:Clone()
localCarScript.Parent = player.PlayerGui
localCarScript.Obj.Value = plane
print("Localscript was inserted")
end
end
end
end)
From there you can use context action services or user input services to detect the key press. Then because the physics can be controlled locally due to how sitting gives the player network ownership over the model/assembly and yeah.
Another way to do it is to use a starter player script like I’m doing if you don’t want to create a local script for every vehicle and instead let one local script decide everything.
Have the local player detect what seat they are sitting on
--In a local script in starter player scripts
local Players = game:GetService("Players")
local player = Players.LocalPlayer
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Seated:Connect(function(active,seatPart)
if active then
local VehicleObjectValue = seatPart:FindFirstChildWhichIsA("ObjectValue")
if VehicleObjectValue then
local VehicleModel = VehicleObjectValue.Value -- model of the vehicleseat
--You can use string Instance values to detect vehicle type as well or perhaps the newer attributes.
--If it is a vehicle, do stuff with the model. locally
else
print("no object value")
end
else
--unseated disconnect controls
end
end)
end)
I believe the second version is better as the setup is faster and less prone to server lag as you don’t have to clone the script from server to client and you can control it from one centralized local script instead of searching every single model. But the first method is simpler and more easily shareable. The method is up to you I guess