When scripting in my movement system, I thought that creating states via Bool Values or Attributes and then configuring them (changing speed or jump power.) through a Script would make scripting way easier. Now, I want to know the most efficient way to do this (as well as with the most customizability) because I don’t think the way I’m currently doing is the fastest.
This is the current code that I’m using to configure everything.
-- States Configuration.
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() or script.Parent
repeat wait(0.1)
until Character:FindFirstChildOfClass("Humanoid") and Character:FindFirstChild("HumanoidRootPart") and Character:FindFirstChild("States")
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local HRP = Character:FindFirstChild("HumanoidRootPart")
local States = Character:FindFirstChild("States")
local function Update()
local WalkSpeed = 16
local JumpPower = 50
local canRotate = true
local canJump = true
-- Disabling Character
if States:FindFirstChild("Stunned") then
WalkSpeed = 5
canJump = false
end
if States:FindFirstChild("Ragdolled") then
WalkSpeed = 0
JumpPower = 0
canRotate = false
canJump = false
end
if States:FindFirstChild("Disabled") then
WalkSpeed = 0
JumpPower = 0
canRotate = false
canJump = false
end
-- Movement
if States:FindFirstChild("Running") then
WalkSpeed = 24.5
end
if States:FindFirstChild("Crouching") then
WalkSpeed = 8
canJump = false
end
Humanoid.WalkSpeed = WalkSpeed
Humanoid.JumpPower = JumpPower
Humanoid.AutoRotate = canRotate
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,canJump)
end
States.ChildAdded:Connect(function()
Update()
end)
States.ChildRemoved:Connect(function()
Update()
end)
Then, I would make states through firing the event something like this.
StatesFolder.Create:FireServer("Running")
And this is the server code that configures it all.
local RS = game:GetService("ReplicatedStorage")
local RSS = RS.Storage
local RSE = RSS.RemoteEvents
local StatesFolder = RSE.States
-- Remotes
local TempState = StatesFolder.Temp
local CreateState = StatesFolder.Create
local DestroyState = StatesFolder.DestroyState
local CDState = StatesFolder.CooldownState
-- Main Script
TempState.OnServerEvent:Connect(function(Player, State, Duration)
local Char = Player.Character or Player.CharacterAdded:Wait()
local Bool = Instance.new("BoolValue")
Bool.Name = State
Bool.Parent = Char.States
game.Debris:AddItem(Bool, Duration)
end)
CreateState.OnServerEvent:Connect(function(Player, State)
local Char = Player.Character or Player.CharacterAdded:Wait()
local Bool = Instance.new("BoolValue")
Bool.Name = State
Bool.Parent = Char.States
end)
DestroyState.OnServerEvent:Connect(function(Player, State)
local Char = Player.Character or Player.CharacterAdded:Wait()
for i,v in pairs(Char.States:GetChildren()) do
if v.Name == State then
v:Destroy()
end
end
end)
CDState.OnServerEvent:Connect(function(Player, State, Duration)
local Char = Player.Character or Player.CharacterAdded:Wait()
local Bool = Instance.new("BoolValue")
Bool.Name = State
Bool.Parent = Char.Cooldowns
game.Debris:AddItem(Bool, Duration)
end)