How would I create a state machine for my needs?

I would like to create a state machine that acts like a restriction on certain states. Right now I have these states: walking, idle, sprinting, trapped, ziplining. There might be more in the future but I was wondering how I would implement these into a state machine? I want it so each state has certain states that they can transition to like how I can go from walking to idle or sprinting. But, I don’t want certain states to be able to go to others like you cant go from trapped to sprinting or ziplining to walking. Currently, I can’t find any state machine that can help with this.

– Define which states can go where
local Transitions = {
Idle = { “Walking”, “Sprinting” },
Walking = { “Idle”, “Sprinting” },
Sprinting = { “Walking”, “Idle” },
Trapped = { “Idle” },
Ziplining = { “Idle” }
}

local CurrentState = “Idle”

local function CanTransition(toState)
for _, allowed in ipairs(Transitions[CurrentState]) do
if allowed == toState then
return true
end
end
return false
end

local function SetState(toState)
if CanTransition(toState) then
print(“State changed:”, CurrentState, “→”, toState)
CurrentState = toState
else
warn(“Can’t go from”, CurrentState, “to”, toState)
end
end

– Example:
SetState(“Walking”) – works
SetState(“Trapped”) – fails
That’s it:

Transitions table = rules.

CurrentState = what you’re in now.

SetState = tries to change state but blocks invalid ones.