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]
end
end
-- Loads of other functions for drifting, hopping, etc
runService.Stepped:Connect(function()
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
kart:Destroy()
end
if workspace.karts:FindFirstChild(player.Name) == nil then
c.Humanoid.Health = 0
end
end)
kart.Changed:Connect(function()
local kp = kart.Position
c.HumanoidRootPart.Anchored = true
dVariable.Value = direction
end)
end
game.Players.PlayerAdded:Connect(function(p)
p.CharacterAdded:Connect(function(c)
goKart(p)
end)
end)
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