I want to make a stamina bar that changes according to a value stored in a folder inside the player’s character. When I change the stamina, the bar just disappears.
Here’s my script in a frame in startergui.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Stamina = character:WaitForChild("CharacterValues"):WaitForChild("Stamina")
local StaminaBeforeChange = Stamina.Value
Stamina:GetPropertyChangedSignal("Value"):Connect(function()
if StaminaBeforeChange > Stamina.Value then
script.Parent.Size = UDim2.new(StaminaBeforeChange - Stamina.Value / 100, 1, 0, 0)
StaminaBeforeChange = Stamina.Value
else
script.Parent.Size = UDim2.new(Stamina.Value - StaminaBeforeChange / 100, 1, 0, 0)
StaminaBeforeChange = Stamina.Value
end
end)
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Stamina = character:WaitForChild("CharacterValues"):WaitForChild("Stamina")
local StaminaBeforeChange = Stamina.Value
Stamina:GetPropertyChangedSignal("Value"):Connect(function()
local stamina = math.clamp(Stamina.Value, 0, 100)
script.Parent.Size = UDim2.new(stamina / 100, 0, 1, 0)
StaminaBeforeChange = Stamina.Value
end)
Not sure what you mean by resting, but this is my code for the stamina:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local CV = Character.CharacterValues
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local RunningSpeed = 32
local CurrentStamina = CV.Stamina
local Sprinting = CV.Sprinting
Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
while true do
task.wait(1)
if Sprinting.Value == true then
CurrentStamina.Value -= 1
elseif not (Sprinting.Value == true) and CurrentStamina.Value < 100 then
CurrentStamina.Value += 1
end
end
end)
Now the size isn’t changing. Overall updated code:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Stamina = character:WaitForChild("CharacterValues"):WaitForChild("Stamina")
local StaminaBeforeChange = Stamina.Value
Stamina:GetPropertyChangedSignal("Value"):Connect(function()
local stamina = math.clamp(Stamina.Value, 0, 100)
script.Parent.Size = UDim2.new(stamina / 100, 0, script.Parent.Size.Y.Scale, script.Parent.Size.Y.Offset)
StaminaBeforeChange = Stamina.Value
end)
First, we have to get 1% of the size of the frame, so we can multiply it later, we can do this by dividing the frame amount by 100, now we have 1% size of the frame. Now we can multiply this size by the current stamina amount, to get a size range from 0-100, and then set that offset to the bar. And that would net us the result, now we script the stamina system and add type checking so the stamina doesn’t go lower than 0, and boom, we’re done!
local Bar = script.Parent.Frame
local CurrentStamina = 100
local InitialBarSizeX = Bar.Size.X.Offset
while task.wait(0.1) do
CurrentStamina = CurrentStamina - 1
local NewSizeX = (InitialBarSizeX / 100)
local FrameSize = UDim2.fromOffset(NewSizeX * CurrentStamina, Bar.Size.Y.Offset)
Bar.Size = FrameSize
if CurrentStamina <= 0 then
return
end
end
Doesn’t work, I had to edit the script to fit my game (because I already have a stamina script) so i might have made a mistake. Here’s the edited code.
local Bar = script.Parent
local CurrentStamina = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("CharacterValues"):WaitForChild("Stamina")
local InitialBarSizeX = Bar.Size.X.Offset
while true do
local NewSizeX = (InitialBarSizeX / 100)
local FrameSize = UDim2.fromOffset(NewSizeX * CurrentStamina, Bar.Size.Y.Offset)
Bar.Size = FrameSize
end
I have no idea how your studio doesn’t crash in the while loop, add a task.wait(), also make sure your Value is between 0-100, otherwise it won’t work.
Its also recommended to use tweening for the size, as setting the size in a while loop isnt efficient.
local Bar = script.Parent
local CurrentStamina = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("CharacterValues"):WaitForChild("Stamina")
local InitialBarSizeX = Bar.Size.X.Offset
while true do
task.wait()
local NewSizeX = (InitialBarSizeX / 100)
local FrameSize = UDim2.fromOffset(NewSizeX * CurrentStamina, Bar.Size.Y.Offset)
Bar.Size = FrameSize
end
For this kind of things you should use UDim2.fromScale, because with the Scale, there are only values between 0 and 1, where 1 would be covering the whole element or the whole screen.
In this case, the stamina value is a number between 0 - 100 so you just should set the frame X size (scale) to “NewSizeX”
local Bar = script.Parent
local CurrentStamina = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("CharacterValues"):WaitForChild("Stamina")
local InitialBarSizeX = Bar.Size.X.Offset
while true do
task.wait()
local NewSizeX = (InitialBarSizeX / 100)
local FrameSize = UDim2.fromScale(NewSizeX, Bar.Size.Y.Scale)
Bar.Size = FrameSize
end