I’ve been trying to make a functioning train. While I understand how to use spinning wheels to achieve forward momentum, those are more physics-intensive than BodyVelocity.
What I don’t understand is how to use BodyVelocity. The only free model available in Studio is (https://www.roblox.com/library/3111261455/Body-velocity-train-base-WORKING), may or may not be leaked; apparently made by yyrebrblx.
My main issue is generally being confused with how to make this. I have tried to search, but I can’t really find anything… Nothing I can find anywhere generally helps with this.
Anything helps!
The thing I need most is someone to explain to me how the code works; and/or point me to a good scripting tutorial for Lua
Here’s the code:
bin = script.Parent
eng = { }
st = bin.VehicleSeat
msp = script.MaximumSpeed.Value
st.MaxSpeed = msp
mrs = script.MaximumReverseSpeed.Value --> Maximum Reverse Speed
spt = script.SpeedPerThrottle.Value --> Speed Per Throttle. This is how much each notch on the throttle changes
mxt = math.ceil(msp / spt) --> This is the maximum throttle
crt = 0 --> Current throttle
csd = 0 --> Current speed
cdr = 0 --> Current direction
drive = false --> Is the train driving. False = not driving, true = driving
plr = nil --> This will change to the current driver
DirTable = {"Reverse", "Neutral", "Foward"}
TrtStp = 0 --> This is used so there is a second or 2 gap in between changing the throttle.
function GetEngines() --> This finds all the engines in the train and then puts them into the table 'eng'
for _, a in pairs(bin:GetChildren()) do
if (a.Name == "Engine") then
--if a:findFirstChild("BodyVelocity") then
local bv = script.BodyVelocity:Clone()
bv.Parent = a
eng[#eng + 1] = a
--end
end
end
end
function GetDirString() return DirTable[cdr + 2]; end
function GetSpeedString(a)
if (a < 0) then return "-"..a.."" end
return msp
end
function GetPlayer()
local a = st.SeatWeld.Part1
if a then
local p = game.Players:findFirstChild(a.Parent.Name)
if p then
return p
end
end
return false
end
function GetHint(p)
-- local a = p:findFirstChild("TrainDriveHint")
local a = p.PlayerGui:findFirstChild("TrainDriveGui")
if a then return a; end
return
end
wait(1)
GetEngines()
h = script.TrainDriveGui:Clone()
h.TextDisplay.Text = "Direction: "..GetDirString().." - Throttle: "..crt.."/"..mxt.." - Speed: "..GetSpeedString(csd).."/"..GetSpeedString(mrs)
h2 = h:Clone();
st.ChildAdded:connect(function ()
drive = true
--[[local cam = script.Cam:Clone()
cam.Disabled = false
if GetPlayer() then
cam.Parent = GetPlayer().Backpack
end]]
end)
st.ChildRemoved:connect(function ()
drive = false
if GetHint(plr) then
GetHint(plr).Parent = nil
h = script.TrainDriveGui:Clone()
h2 = h:Clone()
end
end)
while true do
wait(0.05)
if drive then
local p = GetPlayer()
if p then
plr = p
local gh = GetHint(p)
if not gh then h2.Parent = p.PlayerGui end
local h2 = GetHint(p)
--Now to calculate the current throttle
local trt, dir = st.Steer, st.Throttle
if (TrtStp > 0) then
TrtStp = TrtStp - 0.5
else
if (trt == -1) then
if (crt > 0) then crt = crt - 1; TrtStp = 1; end
elseif(trt == 1) then
if (crt < mxt) then crt = crt + 1; TrtStp = 1; end
end
end
--Throttle calculated, now get the direction
cdr = cdr + dir
if (cdr > 1) then cdr = 1 end
if (cdr < -1) then cdr = -1 end
--Direction done, now work out the speed
if (crt == 0) then
if (csd < 0) then
if (csd + 0.5 > 0) then
csd = 0
else
csd = csd + 0.5
end
elseif (csd > 0) then
if (csd - 0.5 < 0) then
csd = 0
else
csd = csd - 0.5
end
end
end
if (crt > 0) then
if (cdr == -1) then
if (csd - (crt / 10) < (spt * crt) * -1) then
csd = (spt * crt) * -1
else
csd = csd - (crt / 10)
end
csd = csd - (crt / 10)
if (csd < (spt * crt) * -1) then csd = csd + 0.5 end
if (csd < (mrs * -1)) then csd = mrs * -1 end
if (csd > 0) then csd = csd * -1 end
elseif (cdr == 1) then
if (csd + (crt / 10) > spt * crt) then
csd = spt * crt
else
csd = csd + (crt / 10)
end
if (csd > spt * crt) then csd = csd - 0.5 end
if (csd > msp) then csd = msp end
if (csd < 0) then csd = csd * -1 end
end
end
for _, a in pairs(eng) do
a.BodyVelocity.velocity = a.CFrame.lookVector * csd
end
h2.TextDisplay.Text = "Direction: "..GetDirString().." - Throttle: "..crt.."/"..mxt.." - Speed: "..csd.."/"..GetSpeedString(mrs)..""
end
end
end
Thanks!
-0rcaa