Why does my controller script get choppy/laggy over time?

I’m making a go-kart racing game like Mario Kart in which the karts are controlled by a script in ServerScriptService. Here’s a simple version of it with all the details necessary:

local controlFunction = script.control_function.Value
local driftFunction = script.drift_function.Value
local hopFunction = script.hop_function.Value
local clientHopFunction = script.client_hop.Value

local runService = game:GetService("RunService")

local function goKart(player)
	local direction = 0 
	local speed = 0 
	local drag = 0.95
	local acceleration = 1.25
	local turnSpeed = 1
	local maxTurnSpeed = 1

	local maxSpeed = 125

	local w = false
	local a = false
	local s = false
	local d = false
	local space = false

-- Loads of variables which track many stuff

	local c = player.Character
	local h = 7

	fix(c) -- A custom function which just makes the character anchored and platformstand

	local kart = Instance.new("Part", workspace.karts)
	kart.Name = player.Name
	kart.Size = Vector3.new(h,h,h)
	kart.Shape = Enum.PartType.Ball
	kart.Material = Enum.Material.DiamondPlate
	kart.Position = c.HumanoidRootPart.Position

	local physProperties = PhysicalProperties.new(density, friction, elasticity, frictionWeight, elasticityWeight) -- Bunch of custom properties for the kart

	kart.CustomPhysicalProperties = physProperties

	local dVariable = Instance.new("NumberValue",kart)
	dVariable.Name = "direction"

	-- other variables

	kart.Transparency = 1

	controlFunction.OnServerInvoke = function(temp, booleens, p)
		if p == player.Name then
			w = booleens[1]
			a = booleens[2]
			s = booleens[3]
			d = booleens[4]
			space = booleens[5]

	-- Loads of other functions for drifting, hopping, etc

		local v = Vector3.new(math.cos(direction/180*math.pi) * speed, kart.AssemblyLinearVelocity.Y, math.sin(direction/180*math.pi) * speed)	-- What direction to move the kart based on its direction, a value between 0 and 360 degrees
		kart.AssemblyLinearVelocity = v
		kart.AssemblyAngularVelocity = Vector3.new(0,0,0)

		local kartPos = kart.Position

		if c.Humanoid.Health == 0 then

		if workspace.karts:FindFirstChild(player.Name) == nil then
			c.Humanoid.Health = 0

		local kp = kart.Position
		c.HumanoidRootPart.Anchored = true

		dVariable.Value = direction


Here’s a video of it in action:

As you can see it’s pretty good, but after it’s been running for some time (few minutes, gave or take) It becomes incredibly choppy and laggy, as seen here:

Why does this happen? How do I fix it?
Any help and feedback appreciated, thanks :slight_smile:

1 Like

I see you anchor the Rootpart, are you sure this itself isn’t the cause of the freezing?

1 Like

I removed it just to be safe, and no, the choppy behavior still occurs. Figured as much to be honest, I know it’s got to relate to my awful code lmao

I couldn’t make what you are so that’s a +1, but as I see it these 2 are all I could come up with:

  • Possible lack of networking (if you didn’t set network-owner to the player) could cause auto-network to freeze while changing (doubt it)

  • Stepped may possibly be causing the problems as if I’m assuming correctly could freeze during a spike of lag (or just has something to do with lagspikes in general)