Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make the stamina bar(Gui)'s size in the x axis go lower when the player runs?

I am trying to make a stamina bar lower when the player runs in this case. The code I am using is this:

game.ReplicatedStorage.RemoteEvent:FireServer()

game:GetService("UserInputService").InputBegan:Connect(function(input, event)
	local StaminaFrame = game.Players.LocalPlayer.PlayerGui.VisibleStatus.Frame.Stamina.Frame.Frame
	local Level = game.Players.LocalPlayer:WaitForChild("Stats").Level
	local Stamina = game.Players.LocalPlayer:WaitForChild("Stats").Stamina
	local StaminaMax = Stamina.Value*Level.Value
	local StaminaMin = 0
	if input == Enum.KeyCode.W and input == Enum.KeyCode.LeftShift then
		StaminaFrame.Size = UDim2.new(Stamina.Value/StaminaMax, 0, 1, 0)
	end
end)

I need help with the "StaminaFrame.Size = UDim2.new(Stamina.Value/StaminaMax, 0, 1, 0)" part. It doesn't seem to work as needed and I am not skilled enough to find a solution. If any of ya'll could help, that would be very nice of you. Thanks in advance.

1 answer

✓ Accepted answer

Added some variables. If you put your stamina gui inside the inputbegan, it only happens once so put it inside a loop like while wait or something so it auto updates.

local w = false;
local ls = false;

game:GetService("UserInputService").InputBegan:Connect(function(input, event)
	if input.KeyCode == Enum.KeyCode.W then
		w = true
	end
	if input.KeyCode == Enum.KeyCode.LeftShift then
		ls = true
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(input, event)
	if input.KeyCode == Enum.KeyCode.W then
		w = false
	end
	if input.KeyCode == Enum.KeyCode.LeftShift then
		ls = false
	end
end)

while wait(1) do
	local StaminaFrame = game.Players.LocalPlayer.PlayerGui.VisibleStatus.Frame.Stamina.Frame.Frame
    local Level = game.Players.LocalPlayer:WaitForChild("Stats").Level
    local Stamina = game.Players.LocalPlayer:WaitForChild("Stats").Stamina
    local StaminaMax = Stamina.Value*Level.Value
    local StaminaMin = 0
    if w == true and ls == true then
        StaminaFrame.Size = UDim2.new(Stamina.Value/StaminaMax, 0, 1, 0)
    end
end

1 comment

0

Thank you very much, it works with some slight edits.

This discussion is preserved as it was. New answers, comments, and votes are closed.