I’m trying to make a combat system and obviously need a combo variable to keep track of what current combo i am in
game.ReplicatedStorage.Remotes.M1.OnServerEvent:Connect(function(player, Mouse)
print("Received M1 from the Client")
local combo = 0
local startTime = tick()
if combo == 3 then
print("M1 Combo!")
combo = 0
end
if tick() - startTime >= 2 then
combo = 0
end
combo = combo + 1
print(combo)
end)
But it just seems to never go higher than 1 or never add 1 to the combo
I’ve tried adding print debugs to the scripts (as seen under combo = combo + 1), but none of those really helped, any help would be appreciated!
combo
is declared as a local variable inside the event handler, so every time it receives an event, it starts combo
at 0. Instead, combo
and startTime
should be declared outside of the function.
Some other small things are that tick()
will be deprecated soon so os.time()
should be used instead. Additionally, combo = combo + 1
can be shortened to combo += 1
, and game:GetService("{serviceName}")
is recommended over game.{serviceName}
. Below, lastHit
is used instead of startTime
since it tracks the time of the last hit.
The script after all these fixes:
local combo = 0
local lastHit = os.time()
game:GetService("ReplicatedStorage").Remotes.M1.OnServerEvent:Connect(function(player, Mouse)
print("Received M1 from the Client")
combo += 1
if os.time() - lastHit >= 2 then
combo = 0
end
lastHit = os.time()
if combo == 3 then
print("M1 Combo!")
combo = 0
end
print(combo)
end)