I have a mana script and it increases by 15 ever couple of seconds but it has to reach 100 but sometimes if the number wont go up to 100 exactly it will go over 100. If someone can please tell me how to fix this it would be a huge help!
Here is the GUI Mana Script:
local bar = script.Parent.ManaFront
local manaText = script.Parent.ManaText -- Assuming you have a TextLabel named ManaText
local char = game.Players.LocalPlayer.Character
local hum = char:WaitForChild("Humanoid")
local maxMana = hum:WaitForChild("MaxMana")
local mana = hum:WaitForChild("Mana")
local function updateManaDisplay()
local change = mana.Value / maxMana.Value
bar:TweenSize(UDim2.new(change, 0, 1, 0))
manaText.Text = tostring(mana.Value) .. " / " .. tostring(maxMana.Value)
end
mana.Changed:Connect(updateManaDisplay)
maxMana.Changed:Connect(updateManaDisplay)
-- Initialize display
updateManaDisplay()
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local maxMana = hum:WaitForChild("MaxMana")
local mana = hum:WaitForChild("Mana")
local manaToGain = 15
while wait(2) do
if mana.Value < maxMana.Value then
mana.Value += manaToGain
end
end
Use math.min to limit the value. It returns the lowest value from both values inputted.
Code:
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local maxMana = hum:WaitForChild("MaxMana")
local mana = hum:WaitForChild("Mana")
local manaToGain = 15
while task.wait(2) do
mana.Value = math.min(mana.Value + manaToGain, maxMana.Value)
end