Hello, I was wondering whether it’s better to create separate states in a state machine - such as GuardBroken, PerfectBlockStunned, AttackStunned, etc. - or instead use a single state with a sub-attribute like “StunReason” (or something similar).
For example, if a player is stunned due to a perfect block, an attack stun should not be able to override that stun because the perfect block stun has a longer duration. Another idea I had was to store the stun end time, and when applying a new stun, check whether its duration is shorter than the current one - if so, simply return false and do not override it.
Below is my current state machine implementation:
function CombatUtils:SetState(player: Player, newState: string)
local currentState = player:GetAttribute("State") or "Idle"
if not CombatConfig.AllowedTransitions[currentState] then return false end
if not CombatConfig.AllowedTransitions[currentState][newState] then
warn(`Invalid transition: {currentState} → {newState}`)
return false
end
local version = (player:GetAttribute("StateVersion") or 0) + 1
player:SetAttribute("StateVersion", version)
player:SetAttribute("State", newState)
print(`Transiting from: {currentState} → {newState}`)
return true, version
end
function CombatUtils:CanTransition(player, newState)
local character = player.Character
local currentState = character:GetAttribute("State")
local transitions = CombatConfig.AllowedTransitions[currentState]
if not transitions then
return false
end
return transitions[newState] == true
end
-- Table for allowed transitions
AllowedTransitions = {
Idle = {
Attack = true,
AerialAttack = true,
Block = true
},
Attack = {
Idle = true
},
Block = {
Idle = true
},
CritAttack = {},
AerialAttack = {},
RunningAttack = {},
Stunned = {}
}