Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==========================
- ------State Machine-------
- ==========================
- -- Credit // @TaylorsRus
- --StateMachine.lua
- local StateMachine ={}
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Players = game:GetService("Players")
- local Config = ReplicatedStorage:WaitForChild("Config")
- local States = require(Config:WaitForChild("States"))
- local HumanoidStates, CombatStates = States.Humanoid, States.Combat
- local State = ""
- function StateMachine:ChangeState(NewState, Humanoid)
- if not table.find(HumanoidStates, NewState) and not table.find(CombatStates, NewState) and not Humanoid then
- error("Attempted to change state to invalid state enum: "..tostring(NewState))
- end
- State = NewState
- print("State Change:",tostring(NewState))
- end
- function StateMachine:GetState()
- return State
- end
- return StateMachine
- --StateManager.lua
- local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
- local StateManager = Knit.CreateController {
- Name = "StateManager",
- }
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Player = game:GetService("Players").LocalPlayer
- local Config = ReplicatedStorage:WaitForChild("Config")
- local Utility = ReplicatedStorage:WaitForChild("Utility")
- local StateMachine = require(Utility:WaitForChild("StateMachine"))
- local States = require(Config:WaitForChild("States"))
- local HumanoidStates, CombatStates = States.Humanoid, States.Combat
- local LastChange = nil
- local AlreadyCounting = false
- local IDLE_REVERT_TIME = 5
- local ITERATION_INTERVAL = 1
- local function ConfigureStates(Character)
- local Humanoid = Character:WaitForChild("Humanoid")
- Humanoid.StateChanged:Connect(function(_, NewState)
- if NewState ~= Enum.HumanoidStateType.Running then
- StateMachine:ChangeState(NewState, true)
- end
- LastChange = os.clock()
- if AlreadyCounting then return end
- AlreadyCounting = true
- while true do
- task.wait(ITERATION_INTERVAL)
- local DeltaTime = os.clock() - LastChange
- if DeltaTime >= IDLE_REVERT_TIME then
- break
- end
- end
- StateMachine:ChangeState("Idle")
- AlreadyCounting = false
- end)
- StateMachine:ChangeState("Idle")
- end
- function StateManager.KnitInit()
- if Player.Character then ConfigureStates(Player.Character) end
- Player.CharacterAdded:Connect(ConfigureStates)
- end
- return StateManager
- --States.lua
- return {
- Humanoid = {
- "Idle",
- "FallingDown",
- "Ragdoll",
- "GettingUp",
- "Walking",
- "Running",
- "Jumping" ,
- "Dashing",
- "Swimming",
- "Freefall",
- "Flying",
- "Landed",
- "Climbing",
- "Seated",
- "PlatformStanding",
- "Dead",
- "Physics",
- },
- Combat = {
- "Ready",
- "Readying",
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement