How would I be able increase my hit count in CombatHandler?

When I try to increase my hit/count amount in my server script it just stays the same.
image

if not Character:FindFirstChild("Stun") then
		print("WERKKKK")
		CM.CombatStarted(Player, Character, Humrp, Count)
		Count += 1
		print(Count)
	end
	
	if Count == 4 then
		Count = 0
	end
1 Like

I’m assuming the “CombatModule” is unrelated to this, right?

1 Like

Besides a knockback part of it, for when Count is equal to 4.

1 Like

Is there more code from the snippet you provided that is not shown? I believe I will need to see that in order to assist you with your issue.

1 Like
-- \\ Module-Related Variables // --

local CM = require(script.CombatModule)

-- \\ Get-Service Variables // --

local RS = game:GetService("ReplicatedStorage")

-- \\ Events Variables // --

local Combat = RS.Combat

-- \\ Functions // --

Combat.OnServerEvent:Connect(function(Player, Count)
	Count = 0
	local Character = Player.Character
	local Humanoid = Character.Humanoid
	local Humrp = Character.HumanoidRootPart
	
	if Character:FindFirstChild("Stun") then return end
	
	if not Character:FindFirstChild("Stun") then
		print("WERKKKK")
		CM.CombatStarted(Player, Character, Humrp, Count)
		Count += 1
		print(Count)
	end
	
	if Count == 4 then
		Count = 0
	end
	
end)

Really this is it unless you’d like to see the local script aswell which passes Count down.

1 Like

I see why. You are setting Count to 0 everytime the event is fired from the client. Meaning, even though it is increased by 1 on the first execution, on the second execution it is set back to 0.

Move the Count = 0 to somewhere outside of the event listener.

1 Like

How would I do that if on the fired event it needs to be set to something?

1 Like

Try this revised code:

- \\ Module-Related Variables // --

local CM = require(script.CombatModule)

-- \\ Get-Service Variables // --

local RS = game:GetService("ReplicatedStorage")

-- \\ Events Variables // --

local Combat = RS.Combat
local Count = 0 -- moved it here

-- \\ Functions // --

Combat.OnServerEvent:Connect(function(Player, Count)
	-- removed Count = 0
	local Character = Player.Character
	local Humanoid = Character.Humanoid
	local Humrp = Character.HumanoidRootPart
	
	if Character:FindFirstChild("Stun") then return end
	
	if not Character:FindFirstChild("Stun") then
		print("WERKKKK")
		CM.CombatStarted(Player, Character, Humrp, Count)
		Count += 1
		print(Count)
	end
	
	if Count == 4 then
		Count = 0
	end
	
end)
1 Like

It definitely worked, but now gave me a new error.

ServerScriptService.CombatHandler:27: attempt to perform arithmetic (add) on nil and number

I’m assuming that is on the line that states “Count += 1”

Or am I wrong?

Ah, I see why (I missed this before)

You have Count as one of your parameters. I’m not sure if you’re using it or not, but I would assume not.

You need to remove that, so the revised line would be:

Combat.OnServerEvent:Connect(function(Player)

Wish I could give two solutions. Thank you so much.

Glad I was able to help you out!

1 Like
Combat.OnServerEvent:Connect(function(Player, Count)
	local Character = Player.Character
	local HRP = Character.HumanoidRootPart

	if Character:FindFirstChild("Stun") then return end

	CM.CombatStarted(Player, Character, HRP, Count)
	Count = if Count == 4 then 0 else Count + 1
end)

Why check for a child instance named “Stun” twice? local Count = 0 is going to be a state variable (flag) shared by every client connected to the server, if it needs to be player specific then a table value should be used instead.