How can I optimize this script?

When I shoot, my FPS drops, how can I optimize this script?

Local Script:

game.ReplicatedStorage["Выстрел из оружия(верт)"].OnClientEvent:Connect(function(MG1, MG2)
	local bullet = game.ReplicatedStorage.Bullet:Clone()
	bullet.LinearVelocity.VectorVelocity = MG1.CFrame.LookVector * 1000
	bullet.Parent = workspace
	bullet.CFrame = MG1.CFrame
	game:GetService("Debris"):AddItem(bullet, 5)
	bullet.Touched:Connect(function(hit)
		if hit.Name == "Bullet" or hit.Name == "Body" then return end

		local humanoid = hit.Parent and hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			game.ReplicatedStorage["Нанесение урона на литл"]:FireServer(humanoid)
		end
		bullet:Destroy()
	end)
	local bullet2 = game.ReplicatedStorage.Bullet:Clone()
	bullet2.LinearVelocity.VectorVelocity = MG2.CFrame.LookVector * 1000
	bullet2.Parent = workspace
	bullet2.CFrame = MG2.CFrame
	game:GetService("Debris"):AddItem(bullet, 5)
	bullet2.Touched:Connect(function(hit)
		if hit.Name == "Bullet" or hit.Name == "Body" then return end

		local humanoid = hit.Parent and hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			game.ReplicatedStorage["Нанесение урона на литл"]:FireServer(humanoid)
		end
		bullet2:Destroy()
	end)
end)
1 Like

You can clone the bullet once and reuse it for both:

game.ReplicatedStorage["Выстрел из оружия(верт)"].OnClientEvent:Connect(function(MG1, MG2)
	local function createBullet(MG)
		local bullet = game.ReplicatedStorage.Bullet:Clone()
		bullet.LinearVelocity.VectorVelocity = MG.CFrame.LookVector * 1000
		bullet.Parent = workspace
		bullet.CFrame = MG.CFrame
		bullet.CanCollide = false
		game:GetService("Debris"):AddItem(bullet, 5)
		bullet.Touched:Connect(function(hit)
			if hit.Name == "Bullet" or hit.Name == "Body" then return end

			local humanoid = hit.Parent and hit.Parent:FindFirstChild("Humanoid")
			if humanoid then
				game.ReplicatedStorage["Нанесение урона на литл"]:FireServer(humanoid)
			end
			bullet:Destroy()
		end)
	end

	createBullet(MG1)
	createBullet(MG2)
end)
3 Likes

Wow, this really helped me. Thanks again, optimization has become much better

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.