- What does the code do and what are you not satisfied with?
- The code below is a local script that is made for handling player movement. sprinting, crouching, running.
- The problem with the code is it’s very messy and unoptimized.
- What potential improvements have you considered?
- I’ve considered making a module script off of it and handling it that way to make it more organized and readable.
- How (specifically) do you want to improve the code?
- I just want the code to be optimized, and easily be able to alter values.
tdr: I just think its very messy and if you could please give me some tips on how to improve it thx.
localscript:
local KEYBINDS = require(script.Parent.KeyBinds)
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
local debris = game:GetService("Debris")
local camera = workspace.CurrentCamera
local staminaBar = player.PlayerGui:WaitForChild("Main"):WaitForChild("Stamina")
local speed = {default=16, running=22}
local fov = {default=70, running=80}
local stamStat = {depletion=0.02, regen=0.015, defaultRegen=0.015, cooldown=0.1}
local slideStat = {magnitude=25, maxForce=30000, duration=0.7}
local stamina = 1
local oldStam = stamina
local clamp, udim, floor = math.clamp, UDim2.new, math.floor
local tired, running, sliding, fovBounce, canSprint, canSlide = false, false, false, false, true, true
------------------------------------------------------------------------------------------
local animations = {
Crouch = humanoid:LoadAnimation(script.Crouch),
Slide = humanoid:LoadAnimation(script.Slide),
}
local function stopAnimations()
for _, v in pairs(animations) do
v:Stop()
end
end
local function changeFov(value)
coroutine.wrap(function()
fovBounce = not fovBounce
local oldBounce = fovBounce
local increment = 1
if value < camera.FieldOfView then increment=-1 end
for i=camera.FieldOfView, value, increment do
if fovBounce == oldBounce then
wait(0.001)
camera.FieldOfView = i
end
end
end)()
end
local function updateStamina()
if staminaBar:FindFirstChild("Bar") then
if oldStam~=stamina then
oldStam = stamina
staminaBar.Bar:TweenSize(udim(stamina,0,1,0), "Out", "Linear", 0.1, true)
staminaBar.Text.Text = floor(stamina*100) .. "%"
end
if canSprint then
staminaBar.Bar.BackgroundColor3 = Color3.fromRGB(0, 187, 255)
else
staminaBar.Bar.BackgroundColor3 = Color3.fromRGB(0, 133, 181)
end
end
end
local function toggleRun(enabled)
if enabled then
if stamina>0.25 then
running = true
humanoid.WalkSpeed = speed.running
changeFov(fov.running)
end
else
running = false
humanoid.WalkSpeed = speed.default
changeFov(fov.default)
end
end
local function manipulateStamina(add, amount)
if add then
stamina=clamp(stamina+amount, 0, 1)
else
stamina=clamp(stamina-amount, 0, 1)
if stamina<=0 then
stamina=0
tired = true
toggleRun(false)
updateStamina()
wait(5)
tired = false
end
end
end
local function slide()
canSlide, canSprint, sliding = false, false, true
toggleRun(false)
manipulateStamina(false, 0.25)
humanoid.WalkSpeed = 0
local force = Instance.new("BodyVelocity")
force.MaxForce = Vector3.new(1,0,1) * slideStat.maxForce
force.Velocity = character.Head.CFrame.LookVector * slideStat.magnitude
force.Parent = character.HumanoidRootPart
debris:AddItem(force, slideStat.duration)
animations.Slide:Play()
wait(slideStat.duration)
stopAnimations()
canSlide, canSprint, sliding = true, true, false
humanoid.WalkSpeed = speed.default
end
local function toggleCrouch(enabled)
if not sliding then
stopAnimations()
if enabled then
if running and canSlide and not sliding and stamina>0.25 then
slide()
else
canSprint = false
toggleRun(false)
stamStat.regen = stamStat.defaultRegen+.008
humanoid.WalkSpeed = speed.default-6
animations.Crouch:Play()
end
else
stamStat.regen = stamStat.defaultRegen-.008
humanoid.WalkSpeed = speed.default
canSprint = true
toggleRun(false)
end
end
end
uis.InputBegan:Connect(function(input, processed)
if not processed then
if input.KeyCode == KEYBINDS.sprint and canSprint then
toggleRun(true)
elseif input.KeyCode == KEYBINDS.crouch then
toggleCrouch(true)
end
end
end)
uis.InputEnded:Connect(function(input, processed)
if not processed then
if input.KeyCode == KEYBINDS.sprint and canSprint then
toggleRun(false)
elseif input.KeyCode == KEYBINDS.crouch then
toggleCrouch(false)
end
end
end)
game.ReplicatedStorage.Events.EditStamina.OnClientEvent:Connect(function(add, amount)
manipulateStamina(add, amount)
end)
while wait(stamStat.cooldown) do
if running then
manipulateStamina(false, stamStat.depletion)
elseif stamina<1 and not tired then
manipulateStamina(true, stamStat.regen)
end
updateStamina()
end