How Would i disconnect this renderstepped becuase gui Size speeds up if i dont disconnect it

UIS.InputBegan:Connect(function(input, gameProcsessed)
    local Cloned = Player:WaitForChild("PlayerGui"):FindFirstChild("BlockingStamina")
    if on then
        if gameProcsessed then return end
        if equipped == false then return end
        if Dashey then return end
        if input.UserInputType == Enum.UserInputType.MouseButton2 then
            blocking = true
            BlockEvent:FireServer("One")
            playanim:Play()
        end
        RunService.RenderStepped:Connect(function(deltaTime)
            if blocking then
                if not StaminaGone or (StaminaGone and currentstamina > 20) then
                    StaminaGone = false
                    currentstamina = math.clamp(currentstamina - 20 * deltaTime, 0, stamina)

                    if currentstamina == 0 then
                        blocking = false
                        playanim:Stop()
                        BlockEvent:FireServer("Two")
                        StaminaGone = true
                    end
                else
                    BlockEvent:FireServer("Two")
                    playanim:Stop()
                    currentstamina = math.clamp(currentstamina + 10 * deltaTime, 0, stamina)
                end
            else
                currentstamina = math.clamp(currentstamina + 10 * deltaTime, 0, stamina)
            end
            Cloned.Frame.BlockingStamina.Size = UDim2.new(currentstamina/stamina, 0, 1, 0)
        end)
    end
end)

UIS.InputEnded:Connect(function(input, IsTyping)
    if IsTyping then return end
    if not on then return end
    if not equipped then return end
    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        blocking = false
        BlockEvent:FireServer("Two")
        playanim:Stop()
    end
end)

Hey @Ouuter again.

Store it in a variable

local GuiTween

UIS.InputBegan:Connect(function(input, gameProcsessed)
-- .. some code here

        GuiTween = RunService.RenderStepped:Connect(function(deltaTime)
       end)

end)

UIS.InputEnded:Connect(function(input, IsTyping)
-- ...some other code
if GuiTween then
GuiTween:Disconnect()
GuiTween = nil -- remove it
end
end)

1 Like

It still is speeding up. This is what my Script looks like now

local GuiSize

UIS.InputBegan:Connect(function(input, gameProcsessed)
    local Cloned = Player:WaitForChild("PlayerGui"):FindFirstChild("BlockingStamina")
    if on then
        if gameProcsessed then return end
        if equipped == false then return end
        if Dashey then return end
        if input.UserInputType == Enum.UserInputType.MouseButton2 then
            blocking = true
            BlockEvent:FireServer("One")
            playanim:Play()
        end
        GuiSize = RunService.RenderStepped:Connect(function(deltaTime)
            if blocking then
                if not StaminaGone or (StaminaGone and currentstamina > 20) then
                    StaminaGone = false
                    currentstamina = math.clamp(currentstamina - 10 * deltaTime, 0, stamina)

                    if currentstamina == 0 then
                        blocking = false
                        playanim:Stop()
                        BlockEvent:FireServer("Two")
                        StaminaGone = true
                    end
                else
                    BlockEvent:FireServer("Two")
                    playanim:Stop()
                    currentstamina = math.clamp(currentstamina + 5 * deltaTime, 0, stamina)
                end
            else
                currentstamina = math.clamp(currentstamina + 5 * deltaTime, 0, stamina)
            end
            Cloned.Frame.BlockingStamina.Size = UDim2.new(currentstamina/stamina, 0, 1, 0)
        end)
    end
end)

UIS.InputEnded:Connect(function(input, IsTyping)
    if IsTyping then return end
    if not on then return end
    if not equipped then return end
    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        blocking = false
        BlockEvent:FireServer("Two")
        playanim:Stop()
        if GuiSize then
            GuiSize:Disconnect()
            GuiSize = nil
        end
    end
end)

actually its speeding up sometimes when i press something other then RightClick

Do you know what could be causing this because if i click Mousebutton1 or press any other key it speeds it up

The GUI tween isn’t wrapped inside the mouse button 2 if statement, any button pressed will trigger the gui tween but only mouse button2 released will disconnect the gui tween.

1 Like

Could i make a function and then put that inside a runservice it would work?

Just wrap the guiSize inside the if statement? Edit: Is this what you mean by putting function in a runservice events? If so then yes.

local guiRenderstepFunction = function(deltaTime)
            if blocking then
                if not StaminaGone or (StaminaGone and currentstamina > 20) then
                    StaminaGone = false
                    currentstamina = math.clamp(currentstamina - 10 * deltaTime, 0, stamina)

                    if currentstamina == 0 then
                        blocking = false
                        playanim:Stop()
                        BlockEvent:FireServer("Two")
                        StaminaGone = true
                    end
                else
                    BlockEvent:FireServer("Two")
                    playanim:Stop()
                    currentstamina = math.clamp(currentstamina + 5 * deltaTime, 0, stamina)
                end
            else
                currentstamina = math.clamp(currentstamina + 5 * deltaTime, 0, stamina)
            end
            Cloned.Frame.BlockingStamina.Size = UDim2.new(currentstamina/stamina, 0, 1, 0)
        end

UIS.InputBegan:Connect(function(input, gameProcsessed)
    local Cloned = Player:WaitForChild("PlayerGui"):FindFirstChild("BlockingStamina")
    if on then
        if gameProcsessed then return end
        if equipped == false then return end
        if Dashey then return end
        if input.UserInputType == Enum.UserInputType.MouseButton2 then
            blocking = true
            BlockEvent:FireServer("One")
            playanim:Play()
        GuiSize = RunService.RenderStepped:Connect(guiRenderstepFunction )--Put it inside here
        end
    end
end)

Use variables like here in order to avoid confusion with if statement nesting.

1 Like

When i disconnect it. I can no longer gain the stamina over time. and if i hold right click while its still going down it goes back up.

Sounds like you need two runservice events, one for updating the Gui to the current stamina

local updateGuiFunction = function()
            Cloned.Frame.BlockingStamina.Size = UDim2.new(currentstamina/stamina, 0, 1, 0)
end

and another one for draining the stamina during blocking.

Edit: I think you can just put the renderstep connection outside the input began to have it always running.

1 Like

I get error code

Players.Ouuter.Backpack.Better Default Saber.Function:158: attempt to index nil with 'Frame'

This is what my script looks like atm

local GuiSize 

local updateGuiFunction = function(deltaTime)
    local Cloned = Player:WaitForChild("PlayerGui"):FindFirstChild("BlockingStamina")
    currentstamina = math.clamp(currentstamina + 10 * deltaTime, 0, stamina)
    Cloned.Frame.BlockingStamina.Size = UDim2.new(currentstamina/stamina, 0, 1, 0)
end

RunService.RenderStepped:Connect(updateGuiFunction)

local guiRenderstepFunction = function(deltaTime)
    local Cloned = Player:WaitForChild("PlayerGui"):FindFirstChild("BlockingStamina")
    if blocking then
        if not StaminaGone or (StaminaGone and currentstamina > 20) then
            StaminaGone = false
            currentstamina = math.clamp(currentstamina - 30 * deltaTime, 0, stamina)

            if currentstamina == 0 then
                blocking = false
                playanim:Stop()
                BlockEvent:FireServer("Two")
                StaminaGone = true
            end
        else
            BlockEvent:FireServer("Two")
            playanim:Stop()
        end
    end
    Cloned.Frame.BlockingStamina.Size = UDim2.new(currentstamina/stamina, 0, 1, 0)
end

UIS.InputBegan:Connect(function(input, gameProcsessed)
    local Cloned = Player:WaitForChild("PlayerGui"):FindFirstChild("BlockingStamina")
    if on then
        if gameProcsessed then return end
        if equipped == false then return end
        if Dashey then return end
        if input.UserInputType == Enum.UserInputType.MouseButton2 then
            blocking = true
            BlockEvent:FireServer("One")
            playanim:Play()
            GuiSize = RunService.RenderStepped:Connect(guiRenderstepFunction)
        end
    end
end)

UIS.InputEnded:Connect(function(input, IsTyping)
    if IsTyping then return end
    if not on then return end
    if not equipped then return end
    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        blocking = false
        BlockEvent:FireServer("Two")
        playanim:Stop()
        if GuiSize then
            GuiSize:Disconnect()
            GuiSize = nil
        end
    end
end)

I actually fixed it. I did a little testing with the UpdateGuiFunction.

1 Like