Projectile Spamming Delaying the Entire Game

I’ve trying to make a thing where you can shoot projectiles through a key. And you can spam it alot with little cooldown between each shot.

But whenever I spam it too much, it delays the game alot. For example, when I spam it a lot for a while, the projectile usually phases through the target who was supposed to be damaged, missing entirely.

Here’s an example of what’s happening:
https://streamable.com/ayah6c
The quote system was meant to be a taunt. Before the latency begins, the words appear fast and smoothly, and the knives land perfectly, dealing damage. But when I begin to spam it, there’s an obvious latency for each word to appear, and later on, the knives begin to phase through the first target, hitting the ones behind, sometimes even phasing through all of them. I tried including it into a .Touched function, which didn’t work out as expected. And I tried adding it into the debris service, which still didn’t work.

Here’s the example of my code, I’m not good at lua, so please tell me where did I do wrong.

cf = CFrame.new
ang = CFrame.Angles
rd = math.rad
rd2 = math.random

game:GetService("ReplicatedStorage").MIHKnife.OnServerEvent:Connect(function(player)
	local chr = player.Character
	local rarm = chr["Right Arm"]
	local hed = chr["Head"]
	
	local bruh = math.random(-1.5, 1.5)
	local bruh2 = math.random(-1.5, 1.5)
	local knife = game.ServerStorage.MIHKnife
	thrown = true
	local knoive = knife:Clone()
	knoive.Parent = game.Workspace
	knoive.CFrame = chr.HumanoidRootPart.CFrame * cf(bruh, bruh2, -3) * ang(rd(30), rd(0), rd(0))
	game.Debris:AddItem(knoive, 5)
	
	local yeet = Instance.new("BodyVelocity")
    yeet.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
    yeet.P = math.huge
    yeet.Velocity = chr.HumanoidRootPart.CFrame.lookVector * 75
    yeet.Parent = knoive
	yeet.Name = "Force"
	
	local traveldistance = 0
	
	local function hito(part, partcfr, mag, dmg, debtim, debtime, bodyfdire, effect, grow, color)
	     	     	 --- [insert effects and damaging stuff]
		       end
			end))
	
			traveldistance = 20
	      end
	    end
	  end
	end
	
	coroutine.resume(coroutine.create(function()
	repeat
		wait()
		traveldistance = traveldistance + 1

		if traveldistance >= 20 then
			knoive.Anchored = true
			projectileemerald:Destroy()
		end
		if knoivethen
		hito(knoive, knoive.CFrame * CFrame.new(0, 0, 0) * CFrame.new(math.random(-0.25, 0.25), math.random(-0.25, 0.25), math.random(-0.25, 0.25)) ,3, 10, 0.05, 0.05, projectileemerald.CFrame.lookVector * 10, "rbxassetid://241837157", 0.05, Color3.new(255, 0, 0))
		end
	until knoive == nil
	end))
end)

Thank you!

2 Likes

This seems to me like a case of server physics lag stalling everything else. Maybe try creating the actual projectile, in this case a knife, on the client by having the server call :FireAllClients() on the MIHKnife remote. After that call, the client would create the projectile on their own client, rendering the whole physics issue gone. I would still do basic damage checks on the server, but nothing huge in terms of rendering tasks. It’s hard to tell with just that code if that’s the actual issue, but please do let me know if this helps.

I respect the variable name choices OP.

1 Like

How can I do that, though?

thanks for the respect on the name btw

If the knives are all based on .Touched events, it’ll cause them to lag if they intersect themselves or an object that’s not a Character with a Humanoid, I reccomend if that’s the case, then change it to Raycasting or Region3 since they put less performance strain.

They aren’t. If I changed them to a .Touch event and they will begin to rapidly deal damage over and over even though the projectile is already removed.

I’d imagine this tutorial using fastcast will get you on the right track.

Hey there, this script reminds me of A Bizarre Day’s old projectiles, which caused the game to slow down.

I assume you own a copy of the game.

Just so you know, you shouldn’t use traveldistance, it’s very unreliable and the main reason your game will slow down, instead you can use a for loop.

Once finished, just remove the projectile.

To prevent the damage from spamming, simply add a value to check.

So when a hito() is called, you can simply write down “Hit = 1”.

for i = 1, 40 do
		wait(0.03)
		hito(stuff)
	end

hito(stuff)
if Hit >= 1 then return end
--Your code.
Hit = 1

Heeey! This certainly reminds me of a game that I develop for!
Must surely be a coincidence because we once had the same exact script for our MiH’s Knife Projectile too!

Anyways, in order to solve your very own unique script’s issue, you must make sure that there’s no memory leaks. :slight_smile:

I’ll keep in tab with your progress for your game too! I’m very interested. ;o

PS: I’m very interested in purchasing that STW.

1 Like

It looks like you’re constantly changing the projectile’s position manually instead of letting the physics engine deal with it. I’m not sure about the performance implications of the method you’re doing currently, since I myself have never really focused on optimizations, however, I would use BodyMovers or the more recent attachment based forces.

They’re easier and would definitely make your script more tidy since I can barely tell what’s happening.

If it’s not caused by physics and script updates (both can be seen in the microprofiler) then I’d imagine it’s your output… you have a glaring issue caused on this line:
if knoivethen, which, if this is the actual code in your script, I’d imagine it’s spamming the output which really bogs the game down after some time.

Oh no, no. You see, the CFrame/Position arguments displayed in the bottom are not to reposition the knife. It is actually to detect nearby enemies. The other arguments to the right, you see, are the magnitudes and damage values. I’ve solved that issue myself by completely rewriting that big ol’ chunk right there. :upside_down_face:

1 Like

Figures… sounds kind of jank though. When you pass in tons of obscure function arguments it’s kind of a given that people wouldn’t understand it :stuck_out_tongue:

1 Like

Yeah. It’s a simple solution, I don’t know how this respectable individual here managed to replicate A Bizarre Day’s same exact issues from a while back when the game’s source code was leaked around October. :upside_down_face:

2 Likes

yikes, it’s almost like this script was taken exactly from ABD copies! hmmmmmm :thinking:

How often are the remotes firing?
If its a lot, i’d reccomend changing the math.random into something else.
Spam of it can cause lag.

I’m not the best at LUA but it may be your animation that’s playing to fast I’m not entirely sure.