Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Player pauses when they let go of shift before stamina bar runs out. Can anyone help?

Asked by 3 years ago
Edited 3 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

I have a stamina system in my game but for some reason when you stop pressing shift before all of the stamina is gone you get stuck in place and can only jump. I don't get any errors from the scripts.

The script has 2 parts, 1 for the server and 1 for the client.

Here is the client one:

local UserInputService = game:GetService("UserInputService")
local ReplicaitedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        ReplicaitedStorage.RemoteEvents.Sprint:FireServer("Began")
    end
end)

UserInputService.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        ReplicaitedStorage.RemoteEvents.Sprint:FireServer("Ended")
    end
end)


ReplicaitedStorage.RemoteEvents.StaminaUpdate.OnClientEvent:Connect(function(stamina, maxStamina)
    Players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new((stamina / maxStamina) * .2, 78, 0.037, 0)
end)

Here is the server one:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local RunService = game:GetService("RunService")

local MaxStamina = 700
local staminaRegen = 0.5

local SprintModifer = 1.5
local sprintStaminaCost = 3

local sprintingPlayers = {}


players.PlayerAdded:Connect(function(player)
    local stamina = Instance.new("IntValue" , players)
    stamina.Value = MaxStamina
    stamina.Name = "Stamina"


    stamina.Changed:Connect(function(property)
        ReplicatedStorage.RemoteEvents.StaminaUpdate:FireClient(player, stamina.Value, MaxStamina)
    end)
end)

ReplicatedStorage.RemoteEvents.Sprint.OnServerEvent:Connect(function(player, state)
    local humanoid = player.Character.Humanoid

    if state == "Began" and humanoid:GetState() == Enum.HumanoidStateType.RunningNoPhysics and humanoid.MoveDirection.Magnitude > 0 then
        sprintingPlayers[player.name] = humanoid.WalkSpeed
        humanoid.WalkSpeed = humanoid.WalkSpeed * SprintModifer
    elseif state == "Ended" and sprintingPlayers[player.Name] then
        humanoid.WalkSpeed = sprintingPlayers[players.Name]
        sprintingPlayers[player.name] = nil


    end
end)

RunService.Heartbeat:Connect(function()
    for index, player in pairs(players:GetChildren()) do
        local stamina = players.Stamina
        local name = player.name

        if not sprintingPlayers[name] then
            if stamina.Value > MaxStamina then
                stamina.Value = MaxStamina
            elseif stamina.Value < MaxStamina then
                stamina.Value = stamina.Value + staminaRegen
            end
        else
            if stamina.Value >= sprintStaminaCost then
                stamina.Value = stamina.Value - sprintStaminaCost
            else
                player.Character.Humanoid.WalkSpeed = sprintingPlayers[name]
                sprintingPlayers[name] = nil
            end
        end     
    end
end)


If you could also give a detailed explanation of the solution that would be great. I'm pretty new to scripting with Lua, I only started in March, so please bear with me.

Re-edited by JesseSong!

Answer this question