体力条无缘无故从100变成0,体力值没有任何互动

问题描述 投票:0回答:1

出于某种原因,每当我启动我的体验时,比如一秒或半秒,栏就会从满到空。我也无法与该值进行任何交互。 (例如:每当我尝试创建一个 if 条件,如果它等于或小于 0,那么它就会停止打拳。它可以工作,但是,当我尝试创建一个 if 条件时,它的条件是小于 100 ,等待 0.2 秒,并将耐力值增加 2。)如果我重复直到达到 100,那么无论情况如何,它都会开始增加,没有特殊原因。 也许有人知道答案? 模块脚本:

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

耐力条脚本:

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)

Punch脚本:

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)

希望这将有助于解决这个问题,但我想我只需要在模块脚本中正确设置变量值,我只是不知道如何。

好吧,我现在只是让它以某种方式工作,但只有当它是 100 或 0 时它才会改变,顺便说一句,我更新了此处提供的脚本。

lua roblox luau roblox-studio
1个回答
0
投票

只需将 maxStamina 设置为 100,因为该值永远不会改变。确保更新耐力脚本。

local info = {
    
    stamina = Instance.new("IntValue"),
    maxstamina = 100
    
}
return info
© www.soinside.com 2019 - 2024. All rights reserved.