#I need help making a stamina variable in a local scripts changeable by other scripts

1 messages · Page 1 of 1 (latest)

hard burrow
#

I have this sprint script that is mostly functional, although I'm struggling to make the stamina accessible by other scripts
what I'm thinking of doing is making the globalstamina.value mirror the stamina value and vice versa without causing too much server lag, but idk much about doing that (I'm trying to learn lua for the first time)

local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer
local gui = localPlayer.PlayerGui:WaitForChild("StaminaGUI")
local staminaBar = gui:WaitForChild("StaminaFrame"):WaitForChild("StaminaBar")
local isMoving = false

local UserInputState = Enum.UserInputState

local SPRINT_ACTION = "ShiftKey"

local character = script.Parent.Parent
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

local DownTime = script.Parent.DownTime
local walkSpeed = script.Parent.WalkSpeed
local sprintSpeed = script.Parent.SprintSpeed
local gainRate = script.Parent.GainRate
local drainRate = script.Parent.DrainRate
local IsExhausted = script.Parent.IsExhausted
local Stamina = script.Parent.MaxStamina.Value
local maxStamina = script.Parent.MaxStamina
local isSprinting = script.Parent.IsSprinting
local GlobalStamina = script.Parent.Stamina

local function sprintInput(actionName, inputState, inputObj)
    if inputState == UserInputState.Begin then
        humanoid.WalkSpeed = sprintSpeed.Value
        isSprinting.Value = true
    elseif inputState == UserInputState.End then
        humanoid.WalkSpeed = walkSpeed.Value
        isSprinting.Value = false
    end
end

ContextActionService:BindAction(
    SPRINT_ACTION,
    sprintInput,
    false,
    Enum.KeyCode.LeftShift,
    Enum.KeyCode.RightShift
)

humanoid.Running:Connect(function(speed)
    if speed > 0.5 then
        isMoving = true
    else
        isMoving = false
    end
end)

local function Exhausted()
    local elapsedTime = 0
    humanoidRootPart.Anchored = true
    while IsExhausted.Value do
        local dt = task.wait()
        elapsedTime += dt
        if elapsedTime >= DownTime.Value then
            IsExhausted.Value = false
            Stamina = maxStamina.Value
        end
    end
    humanoidRootPart.Anchored = false
end

RunService.RenderStepped:Connect(function(deltaTime)
    
    
    -- God will 100% bring this shit back to me at the pearly gates
    if IsExhausted.Value then return end
    staminaBar.Size = UDim2.new(Stamina / maxStamina.Value, 0, 1, 0)
    if isSprinting.Value == true then
        if isMoving then
                Stamina -= drainRate.Value * deltaTime
        else
                Stamina += gainRate.Value * deltaTime
        end
    else
            Stamina += gainRate.Value * deltaTime
    end

    Stamina = math.clamp(Stamina, 0, maxStamina.Value)

    if Stamina <= 0  then
            IsExhausted.Value = true
            Exhausted()
    end
end)
hard burrow
bleak halo
hard burrow
#

mind guiding me through ts though?

bleak halo
#

i mind

past trail
#

you can use module scripts

#

something like this:
local module = {}
local DEFAULT_STAMINA = 100 — default stamina
local CURRENT_STAMINA = DEFAULT_STAMINA — current stamina

function module.changeStamina(newStamina: number)
if newStamina >= 0 and newStamina <= DEFAULT_STAMINA then
CURRENT_STAMINA = newStamina
end
end

young sageBOT
#

studio** You are now Level 1! **studio

past trail
#

you can include this modulescript using following:

#

lets say the modulescript is inside starterplayer and the modulescript is called „StaminaMS“

#

local MS = require(game.StarterPlayer.StaminaMS)

#

— you have to require modulescripts to use them

#

— then you can just call functions from this modulescript

#

MS.changeStamina(50)

#

— You can include the MS in multiple localscripts and the Stamina value will be the same for all localscripts

hard burrow
#

it does use module scripts now

#

and remote events

past trail
#

true