22:24:36.246 - Script timeout: exhausted allowed execution time?

Hello! I am working on an FPS Framework, and I have been having an error while working on a reloading system. The error is: [22:24:36.246 - Script timeout: exhausted allowed execution time

Please help me with this! I have no idea what’s going on here!

My Script:

	if shooting == false and WeaponData.Ammo ~= 0 then

		shooting = true
		
		local muzzle = WeaponInHand:WaitForChild("Muzzle")
		local ray = Ray.new(muzzle.CFrame.p, (mouse.Hit.p-muzzle.CFrame.p).unit * 999)
		local part, pos = workspace:FindPartOnRayWithIgnoreList(ray, {cam,char,ignore})
		local damage = 0

		muzzle:WaitForChild("Fire"):Play()
		recoilcf = WeaponData.ShootCFrame

		if part then
			if part.Parent:FindFirstChild("Humanoid") then
				if part.Name == "Head" then
					damage = WeaponData.HeadDamage
				else
					damage = WeaponData.Damage
				end
			end

			Events.Shoot:FireServer(part,pos,damage) --sends value(s) to server
		end
		WeaponData.Ammo = WeaponData.Ammo -1
		print(WeaponData.Ammo)
		wait(60/WeaponData.ShootRate)
		shooting = false
	end
end

Hey Ayphi.

This error usually results from an infinite loop, usually the solution to this is a wait but I noticed you already have one. What value is ShootRate?

It looks like you need a wait after this conditional. If true doesn’t pass for both evaluations for the conditional, you’re gonna run into an infinite loop.

while true do
	if shooting == false and WeaponData.Ammo ~= 0 then

		shooting = true
		
		local muzzle = WeaponInHand:WaitForChild("Muzzle")
		local ray = Ray.new(muzzle.CFrame.p, (mouse.Hit.p-muzzle.CFrame.p).unit * 999)
		local part, pos = workspace:FindPartOnRayWithIgnoreList(ray, {cam,char,ignore})
		local damage = 0

		muzzle:WaitForChild("Fire"):Play()
		recoilcf = WeaponData.ShootCFrame

		if part then
			if part.Parent:FindFirstChild("Humanoid") then
				if part.Name == "Head" then
					damage = WeaponData.HeadDamage
				else
					damage = WeaponData.Damage
				end
			end

			Events.Shoot:FireServer(part,pos,damage) --sends value(s) to server
		end
		WeaponData.Ammo = WeaponData.Ammo -1
		print(WeaponData.Ammo)
		wait(60/WeaponData.ShootRate)
		shooting = false
	end
-- add wait here --
end
1 Like

ShootRate is a Module that changes how fast the gun shoot, it currently is at 400.

I’m missing the top part if it’s a while loop you need to add a wait because it won’t wait if the if statement doesn’t resolve.

Thank you very much! Slows the fire rate a bit but of course I can work around that, thank you for the solution!

1 Like

I recommend taking a look at RunService to use Heartbeat for weapons, instead of using a loop like this one. You can find more info on that here: