I want to make a train movement system.
Inside my Drive seat (inside the first carriage named appropriately, and welded per carriage), I have the following script:
local Velocity = 0
script.Parent.Changed:Connect(function(typEe)
if typEe == "Throttle" then
local Input = script.Parent.Throttle
repeat
Velocity = (Velocity + Input)
if Input * Velocity / math.abs(Velocity) == -1 then
wait(script.Parent.Parent.Parent.Data.Acceleration.Value)
else
wait(script.Parent.Parent.Parent.Data.Acceleration.Value)
end
if Velocity > script.Parent.Parent.Parent.Data.MaxSpeed.Value then
Velocity = script.Parent.Parent.Parent.Data.MaxSpeed.Value
elseif Velocity < 0 then
Velocity = 0
end
until script.Parent.Throttle ~= Input
end
end)
while true do
wait()
vectorpower = script.Parent.Parent.Parent.Data.MaxSpeed.Value * script.Parent.CFrame.LookVector
for i=1, script.Parent.Parent.Parent.Data.Carriages.Value do
local vel = script.Parent.Parent.Parent[i]["Move" .. i].BodyVelocity
vel.MaxForce = Vector3.new(
(vectorpower.X > 0 and vectorpower.X or -vectorpower.X),
(vectorpower.Y > 0 and vectorpower.Y or -vectorpower.Y),
(vectorpower.Z > 0 and vectorpower.Z or -vectorpower.Z)
)
vel.Velocity = Velocity * script.Parent.CFrame.LookVector
end
end
(typEe is spelt that delibarately as type is global variable)
Any idea?