How should I optimize this code, (for projectiles)

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Smooth projectiles

  2. What is the issue? when the server is running for too long or a lot of projectiles were fired they become slower

  3. What solutions have you tried so far? tried doing it through the “FireAllClients” and doing it all on the client side, but that made it a lot worse, hit detection was bad, and projectiles didn’t destroy

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- FPEvent.OnServerEvent:Connect(function(plr)
	
	local proj = game.ServerStorage.TearsModels.Default:Clone()
	
	
	local speed = game.ReplicatedStorage:FindFirstChild(plr.Name).PSpeed.Value
	local distance = game.ReplicatedStorage:FindFirstChild(plr.Name).Distance.Value
	local damage = game.ReplicatedStorage:FindFirstChild(plr.Name).Damage.Value
	
	local velocity = speed * 60
	local lifetime = distance / velocity
	local overtimefall = -0.03 / (distance / 40) 
	
	
	proj.Position = plr.Character.Head.Position
	proj.Anchored = true
	proj.CanCollide = false
	proj.Orientation = Vector3.new(0, plr.Character.Head.Orientation.Y, 0)
	
	proj.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= plr.Name then
			hit.Parent.Humanoid.Health -= damage
			proj:Destroy()
		end
	end)
	
	RNS.Heartbeat:Connect(function(delta_time)
		proj.Position = proj.Position + proj.CFrame.LookVector * speed
		proj.Position = proj.Position + Vector3.new(0, overtimefall, 0)
	end)
	
	proj.Parent = workspace
	
	task.delay(lifetime, function()
		local fall = -0.1
		RNS.Heartbeat:Connect(function(delta_time)
			fall -= 0.02
			proj.Position = proj.Position + Vector3.new(0, fall, 0)
		end)
		task.delay(0.3, function()
			proj:Destroy()
		end)
	end)
	
	
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Use the debris game service its a lot better. Also the other thing that makes it a lot worse is that you are updating the projectile on the server with RunService, causing it to run every single frame for every single bullet. I recommend using LinearVelocity for this, as it uses actual physics (so its more realistic) and you dont have to run custom functions every single frame.

So basically the end result would look something like this: (use this as a template, because i didnt actually test it, its just to give you a better understanding of the new script layout with the solutions implemented)

FPEvent.OnServerEvent:Connect(function(plr)
	local proj = game.ServerStorage.TearsModels.Default:Clone()
	
	local speed = game.ReplicatedStorage:FindFirstChild(plr.Name).PSpeed.Value
	local damage = game.ReplicatedStorage:FindFirstChild(plr.Name).Damage.Value
	
	local lifetime = 30 -- SECONDS
	
	proj.Position = plr.Character.Head.Position
	proj.Anchored = false
	proj.CanCollide = false
	proj.Orientation = Vector3.new(0, plr.Character.Head.Orientation.Y, 0)
	
	proj.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= plr.Name then
			hit.Parent.Humanoid.Health -= damage
			proj:Destroy()
		end
	end)
	
	local velocityConstraint = Instance.new("LinearVelocity")
	velocityConstraint.VectorVelocity = Vector3.zAxis * speed -- (Adjust the axis as needed)
	velocityConstraint.ForceLimitsEnabled = false

	proj.Parent = workspace
	
	game:GetService("Debris"):AddItem(projectile, lifetime)
end)

but how do I keep overtimefall in this case? I want the projectile to slowly fall while it travels

As i said LinearVelocity uses real physics, so it will cause the bullet to fall over time. If you want this effect to be apparent a lot more though, you can edit its CustomPhysicalProperties so that its density is a lot higher so it slows down faster. Its basically a matter of playing around with numbers now to get the perfect effect you want.

So, I tried putting it in, but it’s absolutely not what I am going for.

This is a video of how it should work, but I need to optimize it, Physics is good, but it ruins the whole thing I am trying to do

Oh i overlooked a small detail about LinearVelocity accidentally, to make the part actually “fall off” you would need to update its velocity on the y axis, i forgot this really annoying feature of this constraint. But this basically sets us back to the initial issue: running custom calculations every frame. So i would actually try using the :ApplyImpulse() function on the projectile, completely removing the LinearVelocity to keep the original physics calculations of the object running. This impulse does fall off really fast though, so i actually suggest lowering the density for this solution and make the initial velocity higher. Hopefully this works.

So I tried that but it just doesn’t work like I need it too, I want it to go in a direction, and only after a bit of time (in this case lifetime) it should fall, so physics probably won’t work here, unless I will make the projectile have no gravity, and enable the gravity after the lifetime is over

Yeah that could work, setting the part to massless using its CustomPhysicalProperties then re-enabling it after the lifetime is over using task.delay().

Did you try assemblylinearvelocity?
It has the physics that you need and fall off over time
Make sure you set :SetNetworkOwner to the client for smooth physics

so I just used bodyvelocity to make it move, and that kind of worked, I guess I’ll go with that, also I’ve set :SetNetworkOwner to get rid of the start lag

I don’t recommend using bodyvelocity it’s deprecated

not to be that guy but bodyvelocity is just linearvelocity but deprecated