How to optimize this script, its currently very laggy

so how would i optimize this script?

local module = {}

local plr = game.Players.LocalPlayer
local chr = plr.Character

local hrp = chr:WaitForChild("HumanoidRootPart")
local cam = game.Workspace.CurrentCamera
local arm = game.ReplicatedStorage.FpsArms:Clone()
arm.Parent = cam
local hum = chr:WaitForChild("Humanoid")

local idle = arm.AnimationController:WaitForChild("Animator"):LoadAnimation(game.ReplicatedStorage.Guns.TestSmg.Idle)
local fire = arm.AnimationController:WaitForChild("Animator"):LoadAnimation(game.ReplicatedStorage.Guns.TestSmg.Fire)

local chrparts = {}
for i,v in pairs(chr:GetDescendants()) do
	if v:IsA("BasePart") then
		table.insert(chrparts, v)
	end
end

idle:Play()
function firegun()
	local velocity = 500
	local bullet = Instance.new("Part")
	bullet.Anchored = true
	bullet.Size = Vector3.new(.5,.5,.5)
	bullet.Transparency = 1
	bullet.Color = Color3.new(1, 0.666667, 0)
	bullet.Material = Enum.Material.Neon
	bullet.CFrame = cam.CFrame
	bullet.Parent = workspace
	bullet.Name = "Shot"
	bullet.CanCollide = false
	bullet.CanTouch = false
	local bullet2 = Instance.new("Part")
	bullet2.Anchored = false
	bullet2.Size = Vector3.new(.5,.5,.5)
	bullet2.Transparency = 1
	bullet2.Color = Color3.new(1, 0.666667, 0)
	bullet2.Material = Enum.Material.Neon
	bullet2.CFrame = cam.CFrame
	bullet2.Parent = bullet
	bullet2.Name = "Shotbox"
	bullet2.CanCollide = false
	local hit = false
	local ts = game.ReplicatedStorage.Shot:Clone()
	ts.CFrame = cam.CFrame
	--ts.anchored = false
	ts.Parent = workspace
	bullet2.Touched:Connect(function(p)
		if hit then return end
		if velocity > 498 then return end
		
		if p.Parent:FindFirstChild("Humanoid") and p.Parent ~= workspace and p ~= bullet and p.Parent ~= chr then
			print("hit", p, p.Parent)
			hit = true
			if p.Name == "Head" then
				p.Parent.Humanoid.Health -= math.random(50 * (velocity/300))
			else
				p.Parent.Humanoid.Health -= math.random(30 * (velocity/400))
			end
			
			velocity = 0
			task.wait()
			bullet:Destroy()
			return
		end

		if  p ~= bullet and p.Parent ~= chr then
			print("hit ground")
			hit = true
			velocity = 0
			bullet:Destroy()
		end
	end)
	
	ts.Shot.Touched:Connect(function(p)
		if hit then return end
		if velocity > 499 then return end

		if p.Parent:FindFirstChild("Humanoid") and p.Parent ~= workspace and p ~= bullet and p.Parent ~= chr then
			print("hhit", p, p.Parent)
			hit = true
			if p.Name == "Head" then
				p.Parent.Humanoid.Health -= math.random(50 * (velocity/300))
			else
				p.Parent.Humanoid.Health -= math.random(30 * (velocity/400))
			end

			velocity = 0
			task.wait()
			ts:Destroy()
			bullet:Destroy()
			return
		end

		if  p ~= bullet and p.Parent ~= chr then
			print("hhit ground")
			hit = true
			velocity = 0
			ts:Destroy()
			bullet:Destroy()
		end
	end)

	local drop = 0
	local passedtime = 0
	repeat
		
		bullet2.CFrame = bullet.CFrame
		if velocity < 480 then
			drop = drop + 0.05
		end
		print(velocity)
		
		if velocity < 499 then
			bullet.Transparency = 0
			game:GetService("TweenService"):Create(bullet, TweenInfo.new(0.01), {Position = bullet.Position + (bullet.CFrame.LookVector * velocity/70 ) - Vector3.new(0,drop,0)}):Play()
		else
			game:GetService("TweenService"):Create(bullet, TweenInfo.new(0.01), {Position = bullet.Position + (bullet.CFrame.LookVector * velocity/70)}):Play()
		end
		passedtime = passedtime + 0.015
		velocity -= 1
		ts:Destroy()
		task.wait()
	until velocity == 0 or hit
	if bullet then bullet:Destroy()

	end
	return
end

function module.fire()
	--shake()
	fire:Play()
	
	
	
	local firebullet = coroutine.create(firegun)
	coroutine.resume(firebullet)
end


game:GetService("RunService").RenderStepped:Connect(function(dt)
	arm.main.CFrame = cam.CFrame-- - Vector3.new(0,0.5,0)
	for i,v in pairs(arm:GetDescendants()) do
		if v:IsA("BasePart") then
			if v.Name  == "main" or v.Name == "fp" then
				v.LocalTransparencyModifier = 1
			else
				v.LocalTransparencyModifier = 0
			end

		end

	end
end)

function shake()
	local CameraShaker = require(game.ReplicatedStorage.CameraShaker)
	local camera = game.Workspace.CurrentCamera
	local camShake = CameraShaker.new(Enum.RenderPriority.Camera.Value, function(shakeCf)
		camera.CFrame = camera.CFrame * shakeCf
	end)

	camShake:Start()

	-- Explosion shake:
	camShake:Shake(CameraShaker.Presets.Bump)
end

game:GetService("UserInputService").InputBegan:Connect(function(k, r)
	if r then return end

	if k.UserInputType == Enum.UserInputType.MouseButton1 then
		module.fire()
	end 	
end)

hum.Died:Connect(function()
	arm:Destroy()
	script:Destroy()
end)





return module

this is a locally used module script that handles the firing of a gun, whenever you shoot bullets your framerate will drop a lot. On my computer after around 50 bullets I start to get 10-20 fps, and that could be a serious problem considering some guns will have 200+ ammo. On my very low end laptop even a few shots drops the fps down considerably…

The lag mainly comes from the hit detection method, but the movement of the bullets also cause some lag, though minimal compared to hit detection.

So how would I go about optimizing this?

(yes I know the bullets are client sided, the damage will be handled on the server later, and bullet replication shouldn’t be a problem.)

2 Likes

What is this section for? This is running a loop during every frame, which is probably what’s making it laggy.

game:GetService("RunService").RenderStepped:Connect(function(dt)
	arm.main.CFrame = cam.CFrame-- - Vector3.new(0,0.5,0)
-	for i,v in pairs(arm:GetDescendants()) do
-		if v:IsA("BasePart") then
-			if v.Name  == "main" or v.Name == "fp" then
-				v.LocalTransparencyModifier = 1
-			else
-				v.LocalTransparencyModifier = 0
-			end
		end
	end
end)
2 Likes

im going to move it out of a loop, and test the performance

yea that helped a lot, around 200 bullets work now at 30fps

I would also suggest using partcache for creating parts.

I have found that it greatly improves the performance of my gun.

1 Like

how would this work?

idk what that is ;-;

oh it has docs, i will try and implement it and post back with the results

fps boosted considerably, lag issues completely fixed, but now the bullets seem to be stopping mid air???
(when a bullet hit a part gets created for debug purposes)

its colliding with the ground, if i jump they work just fine… what

edit:
i am literally stupid

edit 2:

fastcast + part cache is the new gun system im using, its good

2 Likes