Hello all,
I just need to stop a train with multiple players from lagging on each client (and the server). The train is automatic and uses AssemblyLinearVelocity to move.
Here’s the main train script:
local dest = script.Parent.Parent.Parent:WaitForChild("Destination")
local ts = game:GetService("TweenService")
local ev = script.Parent:WaitForChild("disableControls")
local motors = script.Parent.Parent:WaitForChild("Essentials"):WaitForChild("Essentials"):WaitForChild("Motors")
local welds = motors.Parent:WaitForChild("Welds")
local main = script.Parent.Parent.Parent
local motors2 = main:WaitForChild("TrainCarriageB"):WaitForChild("Essentials"):WaitForChild("Essentials"):WaitForChild("Motors")
local welds2 = motors2.Parent:WaitForChild("Welds")
local r2 = main:WaitForChild("TrainCarriageB"):WaitForChild("Root")
local touched = false
local function doors(num, side)
if side == "LEFT" then
for i, v in pairs(motors:GetChildren()) do
if v.Name == "LEFT" then
v.TargetPosition = num
end
end
for i, v in pairs(motors2:GetChildren()) do
if v.Name == "LEFT" then
v.TargetPosition = num
end
end
else
for i, v in pairs(motors:GetChildren()) do
if v.Name == "RIGHT" then
v.TargetPosition = num
end
end
for i, v in pairs(motors2:GetChildren()) do
if v.Name == "RIGHT" then
v.TargetPosition = num
end
end
end
end
local function accel()
local dct = script.Parent.Parent:WaitForChild("CheckBox")
local dct2 = dct.Parent.Parent:WaitForChild("TrainCarriageB"):WaitForChild("CheckBox")
for i, v in pairs(motors:GetChildren()) do
v.Enabled = false
end
for i, v in pairs(welds:GetChildren()) do
v.Enabled = true
end
for i, v in pairs(motors2:GetChildren()) do
v.Enabled = false
end
for i, v in pairs(welds2:GetChildren()) do
v.Enabled = true
end
for i, v in pairs(workspace:GetPartsInPart(dct)) do
if v.Parent:FindFirstChild("Humanoid") then
v.Massless = true
--if v.Name == "HumanoidRootPart" then
-- local weld = Instance.new("WeldConstraint")
-- weld.Parent = script.Parent
-- weld.Name = "PLAYERWELD"
-- weld.Part0 = v
-- weld.Part1 = dct
--end
v.Parent:FindFirstChild("Humanoid"):ChangeState(Enum.HumanoidStateType.PlatformStanding)
local plr = game:GetService("Players"):GetPlayerFromCharacter(v.Parent)
if plr then
ev:FireClient(plr, true)
end
end
end
for i, v in pairs(workspace:GetPartsInPart(dct2)) do
if v.Parent:FindFirstChild("Humanoid") then
v.Massless = true
--if v.Name == "HumanoidRootPart" then
-- local weld = Instance.new("WeldConstraint")
-- weld.Parent = script.Parent
-- weld.Name = "PLAYERWELD"
-- weld.Part0 = v
-- weld.Part1 = dct2
--end
v.Parent:FindFirstChild("Humanoid"):ChangeState(Enum.HumanoidStateType.PlatformStanding)
local plr = game:GetService("Players"):GetPlayerFromCharacter(v.Parent)
if plr then
ev:FireClient(plr, true)
end
end
end
task.wait(4)
script.Parent.Anchored = false
r2.Anchored = false
local accel = ts:Create(
script.Parent,
TweenInfo.new(17),
{AssemblyLinearVelocity = script.Parent.CFrame.LookVector * 60}
)
accel:Play()
accel.Completed:Connect(function()
maintain()
end)
end
local decel = ts:Create(
script.Parent,
TweenInfo.new(24),
{AssemblyLinearVelocity = script.Parent.CFrame.LookVector * 8}
)
local stop = ts:Create(
script.Parent,
TweenInfo.new(0.5),
{AssemblyLinearVelocity = script.Parent.CFrame.LookVector * 0}
)
task.spawn(function()
script.Parent.Touched:Connect(function(p)
if p.Name == "s2" or "s1" then
if dest.Value == 1 then
if p.Name == "s1" then
touched = true
end
elseif dest.Value == 2 then
if p.Name == "s2" then
touched = true
end
end
elseif p.Name == "turn" then
end
end)
end)
function maintain()
repeat
script.Parent.AssemblyLinearVelocity = script.Parent.CFrame.LookVector * 60
task.wait(0.1)
print(touched)
until touched == true
decel:Play()
script.Parent.Touched:Connect(function(p)
if p.Name == "STOP" then
decel:Cancel()
stop:Play()
task.wait(4)
doors(-4, "LEFT")
end
end)
touched = false
end
doors(-4, "LEFT")
task.wait(6)
doors(0, "LEFT")
task.wait(4)
accel()
When there are more than 2 players inside the train at once, it begins to move in a slow and halty fashion. Is there any way to fix this?
Thanks!