Custom physics lags out server

alrighty, so im using a little script to spawn particles that do something. i am using a while loop to both check if they hit anything or if they should move forward. also, please, recommend me how i should reduce lag with custom physics because i feel that it may VERY VERY GREATLY impact my future projects. anyway heres the script!!

for i = 0, math.random(10, 11), 1 do
	task.spawn(function()
		local frag = game.ReplicatedStorage.otherstuffs.frag:Clone()
		local width = frag.Size.X + math.random(-10,100)/100
		frag.Size = Vector3.new(width, frag.Size.Y - math.random(-10, 10)/10, width)
		frag.Position = Rocket.Position + Vector3.new(0, 1, 0)
		local deltatime = 0.017

		debris:AddItem(frag, 5)

		local dir = Vector3.new(math.random(-20, 20), 50, math.random(-20, 20))
		local param = RaycastParams.new()
		param.CollisionGroup = "projectile"
		local destroyed = false
		frag.Destroying:Connect(function() destroyed = true end)

		frag.Parent = workspace
		while not destroyed do
			local raycastresult = workspace:Raycast(frag.Position, Vector3.new(dir.Unit.X * 2, dir.Y, dir.Unit.Z * 2) * deltatime, param)
			print("started loop")
			if raycastresult then
				print("found ray")
				frag.Position = raycastresult.Position
				frag.Anchored = false
				local hum = raycastresult.Instance.Parent:FindFirstChildOfClass("Humanoid")
				if hum and game.Players:GetPlayerFromCharacter(hum.Parent) == nil then
					damagemodule.DamageZombie(game.ReplicatedStorage.weapons.RocketLauncher.damage.Value * CreatorTag.Value.Character.damageBUFF.Value, true, "explosion", CreatorTag.Value, hum.Parent, 6)
				end
				return
			else
				print("moved em")
				frag.Position += dir * deltatime

				dir = dir * (1 - 0.05 * deltatime) + Vector3.new(0, -workspace.Gravity * deltatime, 0)
				frag.CFrame = CFrame.lookAt(frag.Position, frag.Position + dir) * CFrame.Angles(-math.pi/2,0,0)

				_, deltatime = runserv.Stepped:Wait()
				task.wait()
			end
		end
	end)
end

by the way, this functions like a charm until i make more of these particles. im sure that the lag is here because of all the moving that i do in the while loop but im not sure how to fix this…

1 Like

bumpbump i am bunping 29characterzz

1 Like

instead of using a while loop, try using a run service connection and disconnect it once the projectile hits something. this is a snippet from a projectile module i wrote a couple months back.

local function updatePosition(deltaTime)
	local velocityVector = currentDirection * velocity
	velocityVector += Vector3.new(0, config.gravity * deltaTime, 0)
	currentDirection = velocityVector.Unit
	currentPosition += velocityVector * deltaTime
	return currentPosition, velocityVector
end

connection = RunService.Heartbeat:Connect(function(deltaTime)
	if tick() - initialTime > config.lifetime then
		connection:Disconnect()
		if cosmeticObject then
			cosmeticObject:Destroy()
		end
		return
	end

	local travelDistance = velocity * deltaTime
	local rayDirection = currentDirection * travelDistance

	local result
	if config.radius then
		result = workspace:Spherecast(currentPosition, config.radius, rayDirection, config.raycastParams)
	else
		result = workspace:Raycast(currentPosition, rayDirection, config.raycastParams)
	end

		if result then
		if config.onHit then
			config.onHit(result)
		end
		connection:Disconnect()
		if cosmeticObject then
			cosmeticObject:Destroy()
		end
	else
		local newPosition, velocityVector = updatePosition(deltaTime)
		currentPosition = newPosition

		if cosmeticObject then
			cosmeticObject.Position = currentPosition
			cosmeticObject.CFrame = CFrame.lookAt(currentPosition, currentPosition + velocityVector)
		end
	end
end)
1 Like

ooh thanks! ill try using heartbeat (never did before)

hmm. it seems that the lag has definitely gotten easier but it still slows the server down a bit. weird. i guess a biiiit of lag wont hurt too much but, please, please help me if its possible to make this better.