How is my car lagging my game?

I’ve been diving deep into my go-kart tycoon game for the past three days, trying to resolve some lag issues, and I could use some help! I’ve been experimenting with server-client interactions, reducing events, and even removing various game elements, but unfortunately, nothing seems to be working.

It’s been a bit of a frustrating part of making the game, especially since this is my first big project in Roblox Studio. I’m desperate to get past this optimization stage of making the game.

If anyone has suggestions or tips on what could be causing the lag, or better, a fix, I would genuinely appreciate it! I’ll share a video and the game download so you can look further into it.

A little note: My brother and I worked on this game together, and during our earlier playtesting, we didn’t notice any significant lag. This issue seems to have popped up after adding some new features to the karts. And more to the game.

The issue seemed to be in the DriftDetect script of the kart.
Every time a property of the seat changed you’d loop thru every single instance of the kart, which isn’t really good for performance.
I fixed this by only running the functions when the Steer or Throttle property have changed and I also changed the name of the tires so that you don’t have to loop thru all of the instances in the kart to find them, instead you can now access them by just doing car.Tire1 or car.Tire2 which is way faster.

-- Variables
local car = script.Parent.Parent
local vehicleSeat = car:WaitForChild("VehicleSeat")

-- Drift configuration
local driftConfig = {
	DriftSoundVolume = 1, -- Volume of the drift sound
	FrictionScale = script.Parent.DriftVal -- Factor to scale friction when drifting
}

-- Drift state variables
local isDrifting = false
local throttleReleased = false

-- Calculate friction adjustment
local function calculateFriction(rootPart)
	local velocity = rootPart.AssemblyLinearVelocity.Magnitude
	return math.clamp((1 - (driftConfig.FrictionScale.Value / 20)) - (velocity / 700), 0, 1)
end

-- Setup tire effects (smoke and sound)
local function setupEffects(tire)
	local smoke = tire:FindFirstChild("TireSmoke")
	if not smoke then
		smoke = game.ServerStorage.TireSmoke:Clone()
		smoke.Name = "TireSmoke"
		smoke.Parent = tire
		smoke.Enabled = false
	end

	local driftSound = vehicleSeat:FindFirstChild("DriftSound")
	if not driftSound then
		driftSound = Instance.new("Sound")
		driftSound.Name = "DriftSound"
		driftSound.Parent = vehicleSeat
		driftSound.Volume = driftConfig.DriftSoundVolume
		driftSound.Looped = true
	end
end

-- Adjust tire physics based on drifting state
local function adjustTirePhysics(tire, frictionValue)
	local properties = tire.CustomPhysicalProperties
	tire.CustomPhysicalProperties = PhysicalProperties.new(
		properties.Density,
		frictionValue, -- Friction
		properties.Elasticity,
		properties.FrictionWeight,
		properties.ElasticityWeight
	)
end

-- Toggle tire effects (smoke and sound) based on drift state
local function toggleEffects(isDrifting)
	
	local smoke = car.Tire1:FindFirstChild("TireSmoke")
	if smoke then smoke.Enabled = isDrifting end
	smoke = car.Tire2:FindFirstChild("TireSmoke")
	if smoke then smoke.Enabled = isDrifting end

	local driftSound = vehicleSeat:FindFirstChild("DriftSound")
	if driftSound then
		if isDrifting then
			if not driftSound.Playing then
				driftSound:Play()
			end
		else
			driftSound:Stop()
		end
	end
end

-- Monitor vehicle seat changes
vehicleSeat.Changed:Connect(function(prop)
	local occupant = vehicleSeat.Occupant
	if occupant then
		local rootPart = occupant.Parent:FindFirstChild("HumanoidRootPart")
		if rootPart then
			if prop == "Throttle" then
				-- Detect throttle release or reapplication
				if vehicleSeat.Throttle == 0 then
					throttleReleased = true
				elseif throttleReleased and vehicleSeat.Throttle > 0 and vehicleSeat.Steer ~= 0 then
					isDrifting = true
					toggleEffects(true)
				elseif vehicleSeat.Throttle > 0 and vehicleSeat.Steer == 0 then
					isDrifting = false
					toggleEffects(false)
				end
			elseif prop == "Steer" then
				-- Stop drifting if player stops steering
				if vehicleSeat.Steer == 0 then
					isDrifting = false
					toggleEffects(false)
				end
			end

			-- Adjust tire physics while drifting
			local frictionValue = isDrifting and calculateFriction(rootPart) or calculateFriction(rootPart) / 0.6
			
			adjustTirePhysics(car.Tire1, frictionValue)
			adjustTirePhysics(car.Tire2, frictionValue)
		end
	end
end)

-- Initialize tire effects
for _, part in ipairs(car:GetDescendants()) do
	if part:IsA("BasePart") and part.Name == "Tire" then
		setupEffects(part)
	end
end
2 Likes

I’ll try it out tomorrow thank you for your reply!

1 Like

It worked, I made a little changes to the other scripts and it worked. But overall the driftdetect was causing the problem, thank you!

1 Like