I have no idea why this script is lagging so hard (in studio there is completely no lag) but it makes it so in a real server it's almost not usable (I used a simular script 2 times).

A few things that could be causing this

  • Remote events and functions
  • Loop is going too many times for a large script
  • Changing CFrames is way too heavy

No idea what the reason is though.

This is the script:

-- Serverscript inside a model in workspace!
local KeyEvent = game:GetService('ReplicatedStorage'):WaitForChild('KeyInput')
local GUPEvent = game:GetService('ReplicatedStorage'):WaitForChild('GetUserPref')

local Car = script.Parent
local Body = Car:WaitForChild('CarBody')

local Sounds = Car:WaitForChild('Sounds')
local MovingSound = Sounds:WaitForChild('Moving')

local StartingSound = Sounds:WaitForChild('Starting')
local IdleSound = Sounds:WaitForChild('Idle')

local Seat = script.Parent:WaitForChild('Seat')
local MaxSpeed = script.Parent:WaitForChild('Speed')

local RotX = Car:WaitForChild('WRX')
local RotY = Car:WaitForChild('WRY')

local BackLight1 = Car:WaitForChild('BackLight1')
local BackLight2 = Car:WaitForChild('BackLight2')

local RotSpeed = 3

CurrentSpeed = 0
BackSpeed = 0.8

MaxSpeed.Value = 1
refreshRate = 0.01

local ForwardKeys = {
	Enum.KeyCode.Up
}

local BackwardsKeys = {
	Enum.KeyCode.Down
}

local RightKeys = {
	Enum.KeyCode.Right
}

local LeftKeys = {
	Enum.KeyCode.Left
}

function findKeyCode(key)
	for index, Key in pairs(ForwardKeys) do
		if key == Key then
			return 'Forward'
		end
	end
	
	for index, Key in pairs(BackwardsKeys) do
		if key == Key then
			return 'Backwards'
		end
	end
	
	for index, Key in pairs(RightKeys) do
		if key == Key then
			return 'Right'
		end
	end
	
	for index, Key in pairs(LeftKeys) do
		if key == Key then
			return 'Left'
		end
	end
	
	return nil
end


function move(key, start)
	if key == 'Forward' and start then
		WF = true
	elseif key == 'Backwards' and start then
		WB = true
	elseif key == 'Right' and start then
		RR = true
	elseif key == 'Left' and start then
		RL = true
	elseif key == 'Forward' and not start then
		WF = false
	elseif key == 'Backwards' and not start then
		WB = false
	elseif key == 'Right' and not start then
		RR = false
	elseif key == 'Left' and not start then
		RL = false
	end
end

WF = false -- Move forward
WB = false -- Move backwards

RR = false -- Rotate Right
RL = false -- Rotate left

function backLights(on)
	if on then
		BackLight1.BrickColor = BrickColor.new('Really red')
		BackLight1.Material = 'Neon'
		
		BackLight2.BrickColor = BrickColor.new('Really red')
		BackLight2.Material = 'Neon'
		
		BackLight1:FindFirstChild('Light').Enabled = true
		BackLight2:FindFirstChild('Light').Enabled = true
	else
		BackLight1.BrickColor = BrickColor.new('Crimson')
		BackLight1.Material = 'SmoothPlastic'
		
		BackLight2.BrickColor = BrickColor.new('Crimson')
		BackLight2.Material = 'SmoothPlastic'
		
		BackLight1:FindFirstChild('Light').Enabled = false
		BackLight2:FindFirstChild('Light').Enabled = false
	end
end

function Layout(Num)
	-- 1) Azerty 2) Qwerty
	if Num == 1 then
		ForwardKeys[2] = Enum.KeyCode.Z
		BackwardsKeys[2] = Enum.KeyCode.S
		
		RightKeys[2] = Enum.KeyCode.D
		LeftKeys[2] = Enum.KeyCode.Q
		
	elseif Num == 2 then
		ForwardKeys[2] = Enum.KeyCode.W
		BackwardsKeys[2] = Enum.KeyCode.S
		
		RightKeys[2] = Enum.KeyCode.D
		LeftKeys[2] = Enum.KeyCode.A
	end
end

function Move()
	local Occupant = Seat.Occupant
	if Occupant then
		local char = Seat.Occupant.Parent
		local plr = game.Players:GetPlayerFromCharacter(char)
		
		if plr then
			if WF == true then
				if CurrentSpeed <= 0 then
					StartingSound:Play()
					wait(StartingSound.TimeLength)
					CurrentSpeed = 0.2
				else
					if MovingSound.IsPlaying == false then
						MovingSound:Play()
					end
				end
				
				backLights(false)				
				
				CurrentSpeed = CurrentSpeed + 0.03
				Body.CFrame = Body.CFrame + (Body.CFrame.lookVector * -CurrentSpeed)
				RotX.Value = -10
			elseif WB == true then
				backLights(true)
				CurrentSpeed = 0
				Body.CFrame = Body.CFrame + (Body.CFrame.lookVector * BackSpeed)
				RotX.Value = 10
			else
				if IdleSound.IsPlaying == false then
					IdleSound:Play()
				end
				if CurrentSpeed >= 0 then
					backLights(true)
					CurrentSpeed = CurrentSpeed -0.15
					Body.CFrame = Body.CFrame + (Body.CFrame.lookVector * -CurrentSpeed)
					RotX.Value = -(CurrentSpeed * 100)
				else
					backLights(false)
					RotX.Value = 0
				end
			end
			
			if RR then
				RotY.Value = 30
				Body.CFrame = Body.CFrame * CFrame.Angles(0,math.rad(-RotSpeed),0)
			elseif RL then
				RotY.Value = -30
				Body.CFrame = Body.CFrame * CFrame.Angles(0,math.rad(RotSpeed),0)
			else
				RotY.Value = 0
			end
		end
	end
end

KeyEvent.OnServerEvent:Connect(function(plr, keyCode, Start)
	if Seat.Occupant then
		local char = Seat.Occupant.Parent
		local player = game.Players:GetPlayerFromCharacter(char)
		
		if plr == player then
			local key = findKeyCode(keyCode)
			
			if key then
				move(key, Start)
			elseif keyCode == Enum.KeyCode.Space then
				
			end
		end
	end
end)

while wait(refreshRate) do
	local Occupant = Seat.Occupant
	if Occupant then
		local char = Seat.Occupant.Parent
		local plr = game.Players:GetPlayerFromCharacter(char)
		local success, message = pcall(function()
			UserPreference = GUPEvent:InvokeClient(plr)
		end)
		if UserPreference then
			if UserPreference == 'AZERTY' then
				Layout(2)
			elseif UserPreference == 'QWERTY' then
				Layout(1)
			end
		end
		Move()
	end
end

7 comments

0

Not sure how to explain this better, it moves but it takes a long time between each time it moves

0

Set the physics to that player

0

To get it smooth for a player, you have to use welds instead of setting CFrames and set the player as networkOwner of the entire model. http://wiki.roblox.com/index.php?title=Weld. Without welds, there's really no way to smooth out CFraming without a custom interpolation script on every client.

0

BodyMovers + Player as network owner are as smooth as it gets...but that's also what causes exploits like cars driving through walls at jailbreak

0

BodyMovers do not really work with welded parts..

0

Oh, ad I am working with welds

0

and*