My combat system has many if statements in it and I’d like to make it neater like waayyyy neater. Is there an alternative that I can do? Here’s an example of how most of my combat systems look:
(fyi I am aware I can spam or as well to make it 1 if statement instead of multiple but it still looks messy)
local function Attack()
if Stunned then
return
end
if Attacking then
return
end
if Speed <= 0 then
return
end
if NotOnGround then
return
end
if CantJump then
return
end
if Frozen then
return
end
animationTrack = Animator:LoadAnimation(script.CombatAnimation1)
animationTrack:Play()
end
Attack()
you could combine all those if-statements into one if Stunned or Attacking or (Speed <= 0) or NotOnGround or CantJump or Frozen then return end
anything neater probably means reducing how many booleans you’re comparing
I’m aware I stated you could do that, but it still would end up messy because I’d need to add it over and over for new things I add that may get in the way of attacking.
then onto reducing how many variables you’re using, couldn’t Stunned and Frozen be the same thing, and if Stunned, wouldn’t Attacking be assumed false? likewise is there a scenario someone could be NotOnGround but also at a Speed <= 0? seems they’d be moving if not standing still
you could dedicate a function to debouncing if you’re talking about having to put this code in many functions, like
local function canAttack()
return --logic here
then in anything that needs to bebounce you could do if not canAttack() then return end
Yeah one of my games has that as well but it still seems really inefficient as well. Maybe the issue is I need to find a way to sum it all up to 1 thing. Perhaps one value that becomes true when player is frozen instead of it being many checks.
I’d try dissolving the amount of debounces needed then. See if there’s anywhere in your gameplay that, say, the player is Stunned but not Frozen, and if there is no difference between the two states (that is, being one or both produces the same restrictions), dissolve it into one debounce
Yeah I think if I have for example a bindableevent that fires with a certain state I can probably add checks to see what kind of state it is before overriding the debounce. Like for example if it’s something that the player can control which in theory wouldn’t have a cooldown to it then I’d need to make that the priority so if a player is hit by an attack and that priority is still going it won’t override the debounce and make it false.