This script is meant to make two IntValues that are put into a player on spawn that represent the MAXMana and the Mana (for spells), if there is a better way to do this, please tell me, bear in mind I am a completely incompetent at scripting. (As you will find out).
game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character)-- When a Player spawns wait(1) local Mana=Instance.New("IntValue") -- New IntValue Mana.Parent = workspace; local MAXMana=Instance.new("IntValue") MAXMana.Parent = workspace; local level= script.Parent.Parent.Parent.Parent.Parent.leaderstats.Lvl.Value MAXMana=level*20 Mana=level*20 -- I have other scripts that would take away Mana upon casting a spell while true do if MAXMana>Mana then -- If your MANA is below your MAX then replenish wait(0.5) Mana=Mana+1 end end while true do if Mana>MAXMana then -- If a glitch/bug occurs and your Mana is ABOVE your MAX make it your max Mana=MaxMana end end end) end)
Try this.
game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:wait() --Waits until the CharacterAdded event has fired. wait(1) local Mana=Instance.new("IntValue") -- New IntValue Mana.Parent = player Mana.Name = "Mana" --Give a name to the IntValue, so you can easily identify it in other scripts. local MAXMana=Instance.new("IntValue") MAXMana.Parent = player MAXMana.Name = "MAXMana" --Give a name to the IntValue, so you can easily identify it in other scripts. local level= player:WaItForChild("leaderstats").Lvl.Value --Wait for leaderstats to become available. MAXMana.Value = level*20 Mana.Value = level*20 -- I have other scripts that would take away Mana upon casting a spell while wait() do if Mana.Value < MAXMana.Value then -- If your MANA is below your MAX then replenish wait(0.5) Mana.Value = Mana.Value + 1 elseif Mana.Value > MAXMana.Value then --If your MANA is above your MAX somehow then set it to the max. Mana.Value = MAXMana.Value else --If any of the above conditions aren't true then. repeat wait() until (Mana.Value > MAXMana.Value) or (not Mana.Value > MAXMana.Value) --Wait until MANA is over or below MAX. end end end)
Make sure the script is in Workspace
or ServerScriptStorage
.
Mana.Parent and MAXMana.Parent should be set to player and at local level you can use player.leaderstats.Lvl.Value. Since you're running it inside a PlayerAdded event, you can easily find anything related to the player.
I suggest you make a model inside player holding these things.
local Stats = Instance.new("Model", player) -- Sets the parent right away local Mana = Instance.new("IntValue", Stats) local MAXMana = Instance.new("IntValue", Stats) Stats.Name = "Stats" Mana.Name = "Mana" MAXMana.Name = "MAXMana"
Then with the while loops, you can do something like this
while wait(0.5) do if Mana.Value<MAXMana.Value then Mana.Value = Mana.Value+1 else Mana.Value = MAXMana.Value end end
Now you have them both in the same loop which repeats itself every 0.5 second. At your script the second loop won't run because it's stuck at the first loop.
Don't keep player values in the workspace because if there is more than one player, then there will be more than one "Mana" and "MAXMana", which will cause scripts that change those values to not work properly. The more efficient way of doing it is by parenting the Mana and MAXMana values to the player when the player enters the game.
game.Players.PlayerAdded:connect(function(Plyr) Plyr.CharacterAdded:wait() --waits until the CharacterAdded event fires local Mana = Instance.New("IntValue") -- New IntValue Mana.Name = "Mana" --Change their name so that other scripts can find the values Mana.Parent = Plyr local MAXMana = Instance.new("IntValue") MAXMana.Name = "MAXMana" MAXMana.Parent = Plyr local level = Plyr:WaitForChild("leaderstats").Lvl.Value --Use WaitForChild to prevent errors MAXMana.Value = level * 20 --Set the value to this number, not the MAXMana itself Mana = level * 20 -- I have other scripts that would take away Mana upon casting a spell while Plyr do --the loop will run as long as the player is in the game wait(0.5) Mana.Value = (Mana.Value >= MAXMana.Value and MAXMana.Value or Mana.Value + 1) --I used a ternary operation to get rid of the second while true do loop end end)
NOTE: Make sure the script is in the Workspace
. If it isn't, it won't work