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

How to make gui bar decrease accordingly to stamina?

I'm trying to make a gui bar that decreases accordingly to the stamina that the player has, but I can't manage to deplete the gui bar according to the player's stamina. Here's the part of the local script:

ReplicatedStorage.RemoteEvents.StaminaUpdate.OnClientEvent:Connect(function(Stamina,MaxStamina)
	Players.LocalPlayer.PlayerGui.Stamina.StaminaBar.Size = UDim2.new(0.1,(Stamina/MaxStamina) * 410,0,31)
	
end)

Server:

Players.PlayerAdded:Connect(function(Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid = Character.Humanoid
	local Stamina = Instance.new("IntValue",Player)
	Stamina.Value = MaxStamina
	Stamina.Name = "Stamina"
	local Animator = Instance.new("Animator",Humanoid)
	
	Stamina.Changed:Connect(function(Property)
		ReplicatedStorage.RemoteEvents.StaminaUpdate:FireClient(Player,Stamina.Value,MaxStamina)
	end)
end)

RunService.Heartbeat:Connect(function()
	for i,Player in pairs(Players:GetChildren()) do
		if not Player:FindFirstChild("Stamina") then
			return 
		end
		local Stamina = Player.Stamina
		local Name = Player.Name
		
		if not SprintingPlayers[Name] then
			if Stamina.Value > MaxStamina then
				Stamina.Value = MaxStamina
			elseif
				Stamina.Value < MaxStamina then
				Stamina.Value = Stamina.Value + StaminaRegeneration
			
			end
		else
			if Stamina.Value >= SprintStaminaCost then
				Stamina.Value = Stamina.Value - SprintStaminaCost
				
			else
				Player.Character.Humanoid.WalkSpeed = SprintingPlayers[Name]
				SprintingPlayers[Name] = nil
			end
		end
	end
end)

What can I do to improve this? I've tried changing code around but to no avail. Please help, i've been hung up on this issue for a while now and am dying to solve it.

1 answer

Instead of using a RemoteEvent to detect a change, you can use .Changed, or run a loop to continuously update the size of the bar depending on the stamina value. Everything UI related is client-sided, so using the server to get the value is unnecessary, as only the server can update server-sided stuff, in which the client can see as well.

Since it's located in the player, we can access it from a localscript in the frame ui;

-> You can use .Changed, and it'll detect a change in the value in which you can tween the size of the frame. Also, you need to use TweenSize to smoothly tween the size of the frame depending on the properties you gave.

Here's my example ->

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local char = player.Character
local playergui = player.PlayerGui

local frame = playergui.Stamina:WaitForChild("StaminaBar")
local stamina = player:WaitForChild("Stamina")

stamina.Changed:Connect(function(val)
frame.Size:TweenSize(UDim2.new(stamina.Value / 100, 0, 1, 0, "In", "Linear", .1, true)
end)

I didn't get a chance to test this, so if there are any errors, please feel free to ask!

2 comments

0

Do you know what exactly is the root of the issue?

0

probably with your placements, but even if we find the issue, firing the client every time it's updating isn't a good thing to do. The answer i posted should work fine with what you need

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