Search code examples
luarobloxluauroblox-studio

Stamina bar going from 100 to 0 for no reason, Stamina value not getting any interactions


For some reason whenever I launch my experience, like a second or a half passes and the bar goes from full to empty. I also can't make any interactions with the value. (example: whenever I try to make a if condition that if it's equal or less than 0, then it stops doing the punches. It works, but, when I try to make an if that has the condition of it being less than a hundred, that waits for 0.2 seconds, and adds 2 to the stamina value.) If I made a repeat until it's 100, then it just starts to increment onto it for no particular reason, no matter the condition. Maybe someone knows the answer? ModuleScript:

local info = {
    
    stamina = Instance.new("IntValue"),
    maxstamina = Instance.new("IntValue"),
    
}
return info

Stamina bar Script:

local ms = require(game.ReplicatedStorage.ModuleScript)
if not ms then
    
    ms = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))
    
end

local TW = game:GetService("TweenService")--Get Tween Service

local Staminabar = script.Parent -- Get The Stamina bar
local function UpdateStaminabar() --Stamina Bar Size Change Function
    local stamina = math.clamp(ms.stamina.Value / ms.maxstamina.Value, 0, 1) --Maths
    local info = TweenInfo.new(ms.stamina.Value / ms.maxstamina.Value,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0) --Tween Info
    TW:Create(script.Parent,info,{Size = UDim2.fromScale(ms.stamina, 1)}):Play() -- Create The Tween Then Play It
end

UpdateStaminabar()--Update The Stamina Bar

ms.stamina:GetPropertyChangedSignal("Value"):Connect(function()
    
    UpdateStaminabar()
    
end)

PunchScript:

local ms = require(game.ReplicatedStorage.ModuleScript)
if not ms then
    
    ms = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))
    
end 
local uis = game:GetService("UserInputService")
local cas = game:GetService("ContextActionService")
local rs = game:GetService("ReplicatedStorage")

local events = rs:WaitForChild("Events")
local hitbox = events:WaitForChild("Hitbox")

local plr = game.Players.LocalPlayer
local character = plr.Character
if not character then
    
    plr:WaitForChild("Character")
    
end
local humanoid = character.Humanoid
local animator = humanoid:WaitForChild("Animator")

local idle = animator:LoadAnimation(script:WaitForChild("idle"))
local jab = animator:LoadAnimation(script:WaitForChild("jab"))
local rightcross = animator:LoadAnimation(script:WaitForChild("rightstraight"))
local lefthook = animator:LoadAnimation(script:WaitForChild("lefthook"))
local righthook = animator:LoadAnimation(script:WaitForChild("righthook"))
local swingsfx = script:WaitForChild("Air swing")

local currentPunch = 0
local currentPunch2 = 0
local debounce1 = false
local debounce2 = false
ms.stamina.Value = 100

local function onInputBegan(input)
    
    if debounce2 == true then return end
    if ms.stamina.Value <= 0 then return end
    
    
    if input.KeyCode == Enum.KeyCode.Q then
        
        ms.stamina.Value -= 20
        debounce2 = true
        humanoid.WalkSpeed = 0.5
        lefthook:Play()
        swingsfx:Play()
        hitbox:FireServer(Vector3.new(4, 5, 3), Vector3.new(4.5, 1), 7.5, 0.15)
        task.wait(0.5)
        humanoid.WalkSpeed = 10
        lefthook:Stop()
        task.wait(6)
        debounce2 = false
        
    end 
    
    if input.KeyCode == Enum.KeyCode.E then

        ms.stamina.Value -= 20
        debounce2 = true
        humanoid.WalkSpeed = 0.5
        righthook:Play()
        swingsfx:Play()
        hitbox:FireServer(Vector3.new(4, 5, 3), Vector3.new(4.5, 1), 7.5, 0.15)
        task.wait(0.5)
        humanoid.WalkSpeed = 10
        righthook:Stop()
        task.wait(6)
        debounce2 = false

    end 
end

local function punch()
    
    if debounce1 then return end
    
    debounce1 = true
    
    if currentPunch == 0 then
        if ms.stamina.Value >= 1 then
            ms.stamina.Value -= 10
            humanoid.WalkSpeed = 0.6
            jab:Play()
            swingsfx:Play()
            hitbox:FireServer(Vector3.new(4, 5, 3), Vector3.new(5.7, 1), 2.5, 0.15)
            task.wait(0.5)
            humanoid.WalkSpeed = 10
            jab:Stop()
            debounce1 = false
        end 
        
    elseif currentPunch == 1 then
        if ms.stamina.Value >= 1 then
            ms.stamina.Value -= 10
            humanoid.WalkSpeed = 0.6
            rightcross:Play()
            swingsfx:Play()
            hitbox:FireServer(Vector3.new(4, 5, 3), Vector3.new(5.7, 1), 2.5, 0.15)
            task.wait(0.5)
            humanoid.WalkSpeed = 10
            rightcross:Stop()
            debounce1 = false
        end
    elseif currentPunch == 2 then
        if ms.stamina.Value >= 1 then
            ms.stamina.Value -= 10
            humanoid.WalkSpeed = 0.6
            jab:Play()
            swingsfx:Play()
            hitbox:FireServer(Vector3.new(4, 5, 3), Vector3.new(5.7, 1), 2.5, 0.15)
            task.wait(0.5)
            humanoid.WalkSpeed = 10
            jab:Stop()
            debounce1 = false
        end
    elseif currentPunch == 3 then
        if ms.stamina.Value >= 1 then
            ms.stamina.Value -= 10
            debounce2 = true
            humanoid.WalkSpeed = 0.6
            rightcross:Play()
            swingsfx:Play()
            hitbox:FireServer(Vector3.new(4, 5, 3), Vector3.new(5.7, 1), 5, 0.15)
            task.wait(0.5)
            humanoid.WalkSpeed = 10
            rightcross:Stop()
            debounce1 = false
            debounce2 = false
        end
    end

    if currentPunch == 3  then
        
        currentPunch = 0
        debounce1 = true
        wait(2)
        debounce1 = false
        
    else
        
        currentPunch += 1
        
    end
    
end

cas:BindAction("Punch", punch, true, Enum.UserInputType.MouseButton1)
uis.InputBegan:Connect(onInputBegan)

Hope that will help solve the case, but I think I just need to set the variables value right in the module script, I just don't know how.

Alright I just made it somehow work now but it only changes when it's either 100 or 0, btw I updated the scripts that are presented in here.


Solution

  • Just set the maxStamina to 100 because that value never changes. Make sure to update the stamina script.

    local info = {
        
        stamina = Instance.new("IntValue"),
        maxstamina = 100
        
    }
    return info