Gun Damage Multiplying

Wsg DevFourm…
I made a gun and whenever I shoot it seems the damage grows exponentially (or linearly)

local function shoot()
	local gui = player.PlayerGui:FindFirstChild("MP5Gui")
	
	if currentAmmo <= 0 or reloading then
		return
	end
	
	HoldTrackFire:Play()

	currentAmmo = currentAmmo - 1
	updateGUI(reloading)
	
	VFXEvent:FireServer(encryptedSecret)

	local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
	local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
	
	SFXEvent:FireServer(reloading, encryptedSecret)

	if part then
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		if not humanoid and part.Parent.Parent:FindFirstChildOfClass("Humanoid") then
			humanoid = part.Parent.Parent:FindFirstChildOfClass("Humanoid")
		end

		if humanoid then
			if part.Name == "Head" then
				damage = damage * headshotMultiplier
			end
			
			mouse.Icon = "rbxassetid://14300473731"
			
			shootEvent:FireServer(humanoid, damage, encryptedSecret)
			
			task.delay(0.1, function() mouse.Icon = "rbxassetid://14300435336" end)
		end
	end
	

	--applyRecoil()
	updateGUI(reloading)
	
end
1 Like

Put the damage variable inside the function.
Also, I added math.ceil() just in case the numbers have decimals.

local function shoot()
	local gui = player.PlayerGui:FindFirstChild("MP5Gui")
	local damage = 20 -- // change the damage

	if currentAmmo <= 0 or reloading then
		return
	end

	HoldTrackFire:Play()

	currentAmmo = currentAmmo - 1
	updateGUI(reloading)

	VFXEvent:FireServer(encryptedSecret)

	local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
	local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

	SFXEvent:FireServer(reloading, encryptedSecret)

	if part then
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		if not humanoid and part.Parent.Parent:FindFirstChildOfClass("Humanoid") then
			humanoid = part.Parent.Parent:FindFirstChildOfClass("Humanoid")
		end

		if humanoid then
			if part.Name == "Head" then
				damage = math.ceil(damage * headshotMultiplier)
			end

			mouse.Icon = "rbxassetid://14300473731"

			shootEvent:FireServer(humanoid, damage, encryptedSecret)

			task.delay(0.1, function() mouse.Icon = "rbxassetid://14300435336" end)
		end
	end


	--applyRecoil()
	updateGUI(reloading)

end
1 Like

Thanks, but that did not address the issue in hand. Let me know if you have any other ideas!

1 Like

I fixed it. If anyone would like the solution here it is:

local function shoot()
	local gui = player.PlayerGui:FindFirstChild("MP5Gui")
	
	if currentAmmo <= 0 or reloading then
		return
	end
	
	HoldTrackFire:Play()

	currentAmmo = currentAmmo - 1
	updateGUI(reloading)
	
	VFXEvent:FireServer(encryptedSecret)

	local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
	local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
	
	SFXEvent:FireServer(reloading, encryptedSecret)

	if part then
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		if not humanoid and part.Parent.Parent:FindFirstChildOfClass("Humanoid") then
			humanoid = part.Parent.Parent:FindFirstChildOfClass("Humanoid")
		end

		if humanoid then
			if part.Name == "Head" then
				damage = damage * headshotMultiplier
			end
			
			mouse.Icon = "rbxassetid://14300473731"
			
			shootEvent:FireServer(humanoid, damage, encryptedSecret)
			
			task.delay(0.1, function() mouse.Icon = "rbxassetid://14300435336" end)
		end
	end
	
	damage = Defdamage

	--applyRecoil()
	updateGUI(reloading)
	
end
1 Like

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