[SOLVED] Preventing trains w/ multiple players from lagging

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!

the jagged movement is probably from network ownership. try experiment around and see what works best.

Not quite familiar with networkownership, would I set just the root part of the train or the entire thing to the player? In addition, which specific player would I set it to, or would I have to use remote events to set it to each player individually on the client side?

the entire train would probably work, and i think you can set network ownership to nil

While slightly mitigated at best, the problem still occurs.

Here’s the updated code:

	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
			if not v.Parent:FindFirstChildOfClass("Humanoid").Seated then
				v.Parent:FindFirstChild("Humanoid"):ChangeState(Enum.HumanoidStateType.PlatformStanding)
			end

			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
	
	for i, v in pairs(main:GetDescendants()) do
		if v:IsA("BasePart") or v:IsA("MeshPart") or v:IsA("Seat") or v:IsA("UnionOperation") then
			v:SetNetworkOwner(nil)
		end
	end
	
	local accel = ts:Create(
		script.Parent,
		TweenInfo.new(17),
		{AssemblyLinearVelocity = script.Parent.CFrame.LookVector * 60}
	)
	
	accel:Play()
	
	accel.Completed:Connect(function()
		maintain()
	end)
end
1 Like

can i see a video of the train acting strangely?

Surprisingly enough, when the issue began to occur and I exited Roblox Studio to get the video recording software, the issue just stopped as soon as I went back into Studio. Do you know why this might be?

I figured it out - for anyone wondering, the issue is dramatically mitigated by just creating invisible Seats welded to your train with the player’s HumanoidRootPart CFrame and forcing players to sit in them.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.