Roblox advanced movement script.

 avatar
unknown
lua
2 years ago
3.3 kB
5
Indexable
-- Variables
local character = game:GetService("Players").LocalPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local userInputService = game:GetService("UserInputService")

local isJumping = false
local isMoving = false
local isSprinting = false
local walkSpeed = 16
local sprintSpeed = 32
local stamina = 100
local maxStamina = 100
local staminaDrainRate = 2
local staminaRecoveryRate = 1

-- Functions
local function move(direction)
    if isMoving then
        if direction == "forward" then
            humanoid.MoveDirection = character.CFrame.lookVector * walkSpeed
        elseif direction == "backward" then
            humanoid.MoveDirection = -character.CFrame.lookVector * walkSpeed
        elseif direction == "left" then
            humanoid.MoveDirection = -character.CFrame.rightVector * walkSpeed
        elseif direction == "right" then
            humanoid.MoveDirection = character.CFrame.rightVector * walkSpeed
        end
    else
        humanoid.MoveDirection = Vector3.new(0,0,0)
    end
end

local function jump()
    if not isJumping and humanoid.FloorMaterial ~= Enum.Material.Air then
        isJumping = true
        humanoid.Jump = true
    end
end

local function sprint()
    if isMoving and not isSprinting then
        isSprinting = true
        humanoid.WalkSpeed = sprintSpeed
    elseif not isMoving and isSprinting then
        isSprinting = false
        humanoid.WalkSpeed = walkSpeed
    end
end

local function drainStamina(amount)
    if stamina > 0 then
        stamina = math.max(stamina - amount, 0)
    end
end

local function recoverStamina(amount)
    if stamina < maxStamina then
        stamina = math.min(stamina + amount, maxStamina)
    end
end

-- Event Listeners
userInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    
    if input.KeyCode == Enum.KeyCode.W then
        isMoving = true
        move("forward")
    elseif input.KeyCode == Enum.KeyCode.S then
        isMoving = true
        move("backward")
    elseif input.KeyCode == Enum.KeyCode.A then
        isMoving = true
        move("left")
    elseif input.KeyCode == Enum.KeyCode.D then
        isMoving = true
        move("right")
    elseif input.KeyCode == Enum.KeyCode.Space then
        jump()
    elseif input.KeyCode == Enum.KeyCode.LeftShift then
        if stamina > 0 then
            isSprinting = true
            humanoid.WalkSpeed = sprintSpeed
        end
    end
end)

userInputService.InputEnded:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    
    if input.KeyCode == Enum.KeyCode.W then
        isMoving = false
        move("forward")
    elseif input.KeyCode == Enum.KeyCode.S then
        isMoving = false
        move("backward")
    elseif input.KeyCode == Enum.KeyCode.A then
        isMoving = false
        move("left")
    elseif input.KeyCode == Enum.KeyCode.D then
        isMoving = false
        move("right")
    elseif input.KeyCode == Enum.KeyCode.LeftShift then
        isSprinting = false
        humanoid.WalkSpeed = walkSpeed
    end
end)

-- Update Loop
game:GetService("RunService").RenderStepped:Connect(function()
    if isSprinting then
        drainStamina(staminaDrainRate)
    else
        recoverStamina(staminaRecoveryRate)
    end
end)
Editor is loading...