Advertisement
TaylorsRus

State Machine System

Aug 26th, 2023
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.69 KB | Software | 0 0
  1. ==========================
  2. ------State Machine-------
  3. ==========================
  4. -- Credit // @TaylorsRus
  5.  
  6. --StateMachine.lua
  7. local StateMachine ={}
  8.  
  9. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  10. local Players = game:GetService("Players")
  11.  
  12. local Config = ReplicatedStorage:WaitForChild("Config")
  13.  
  14. local States = require(Config:WaitForChild("States"))
  15. local HumanoidStates, CombatStates = States.Humanoid, States.Combat
  16.  
  17. local State = ""
  18.  
  19. function StateMachine:ChangeState(NewState, Humanoid)
  20.     if not table.find(HumanoidStates, NewState) and not table.find(CombatStates, NewState) and not Humanoid then
  21.         error("Attempted to change state to invalid state enum: "..tostring(NewState))
  22.     end
  23.    
  24.     State = NewState
  25.     print("State Change:",tostring(NewState))
  26. end
  27.  
  28. function StateMachine:GetState()
  29.     return State
  30. end
  31.  
  32. return StateMachine
  33.  
  34. --StateManager.lua
  35. local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
  36. local StateManager = Knit.CreateController {
  37.     Name = "StateManager",
  38. }
  39.  
  40. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  41. local Player = game:GetService("Players").LocalPlayer
  42.  
  43. local Config = ReplicatedStorage:WaitForChild("Config")
  44. local Utility = ReplicatedStorage:WaitForChild("Utility")
  45.  
  46. local StateMachine = require(Utility:WaitForChild("StateMachine"))
  47. local States = require(Config:WaitForChild("States"))
  48. local HumanoidStates, CombatStates = States.Humanoid, States.Combat
  49.  
  50. local LastChange = nil
  51. local AlreadyCounting = false
  52.  
  53. local IDLE_REVERT_TIME = 5
  54. local ITERATION_INTERVAL = 1
  55.  
  56. local function ConfigureStates(Character)
  57.     local Humanoid = Character:WaitForChild("Humanoid")
  58.     Humanoid.StateChanged:Connect(function(_, NewState)
  59.         if NewState ~= Enum.HumanoidStateType.Running then
  60.             StateMachine:ChangeState(NewState, true)
  61.         end
  62.         LastChange = os.clock()
  63.        
  64.         if AlreadyCounting then return end
  65.         AlreadyCounting = true
  66.        
  67.         while true do
  68.             task.wait(ITERATION_INTERVAL)
  69.             local DeltaTime = os.clock() - LastChange
  70.             if DeltaTime >= IDLE_REVERT_TIME then
  71.                 break
  72.             end
  73.         end
  74.        
  75.         StateMachine:ChangeState("Idle")
  76.         AlreadyCounting = false
  77.     end)
  78.    
  79.     StateMachine:ChangeState("Idle")
  80. end
  81.  
  82. function StateManager.KnitInit()
  83.     if Player.Character then ConfigureStates(Player.Character) end
  84.     Player.CharacterAdded:Connect(ConfigureStates)
  85. end
  86.  
  87. return StateManager
  88.  
  89. --States.lua
  90. return {
  91.     Humanoid = {
  92.         "Idle",
  93.         "FallingDown",
  94.         "Ragdoll",
  95.         "GettingUp",
  96.         "Walking",
  97.         "Running",
  98.         "Jumping" ,
  99.         "Dashing",
  100.         "Swimming",
  101.         "Freefall",
  102.         "Flying",
  103.         "Landed",
  104.         "Climbing",
  105.         "Seated",
  106.         "PlatformStanding",
  107.         "Dead",
  108.         "Physics",
  109.     },
  110.     Combat = {
  111.         "Ready",
  112.         "Readying",
  113.     }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement