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.

2 comments
Do you know what exactly is the root of the issue?
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