Hello! So I am working on a game where you can select a character from a menu, and each character has different health, mana, and powers. I already have two characters implemented, but the mana bar is super buggy. When you first load a game and pick a character, everything works fine, the mana works well. If you reset and pick another character, the mana stays the same as for the other character before you reset. For example, if I load in as character A, who has 250 mana, and use up some mana, the bar is 175/250. If I reset and load in character B, who is supposed to have 200 max mana, the bar shows 175/250 mana. And here’s the fun part. If I reset again, and pick character A again, it shows the correct mana properties for character B. I will also post a video to avoid confusion. Roblox Studio script issue - YouTube
The settings folder contains the Mana value, the Health value, the MaxMana value, and an ID value. Here’s the script for the mana bar:
--variables
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local manabar = script.Parent:WaitForChild("ManaBar")
local settings = nil
while settings == nil do
if not player:FindFirstChild("Settings") then
print ("lily")
else
settings = player.Settings
print ("poo")
end
wait()
end
local maxmana = settings:WaitForChild("MaxMana")
local mana = settings:WaitForChild("Mana")
local PinkBar = script.Parent.ManaBar.PinkBar
--Update the Mana Bar
game:GetService("RunService").RenderStepped:Connect(function()
PinkBar.Size = UDim2.new(mana.Value/maxmana.Value, 0, 1, 0)
manabar.ManaStatus.Text = mana.Value.."/"..maxmana.Value
end)
--variables
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local manabar = script.Parent:WaitForChild("ManaBar")
local settings = nil
spawn(function() -- make it so the while loop wont yield
while settings == nil do
if not player:FindFirstChild("Settings") then
print ("lily")
else
settings = player.Settings
print ("poo")
end
wait()
end
end)
local PinkBar = script.Parent.ManaBar.PinkBar
--Update the Mana Bar
game:GetService("RunService").RenderStepped:Connect(function()
local maxmana = settings:WaitForChild("MaxMana") --move these to the loop so it updates every time
local mana = settings:WaitForChild("Mana")
PinkBar.Size = UDim2.new(mana.Value/maxmana.Value, 0, 1, 0)
manabar.ManaStatus.Text = mana.Value.."/"..maxmana.Value
end)
Unfortunately, it has the exact same issue, and it also gives me an error: Players.domnulcheie.PlayerGui.ManaBarGUI.LocalScript:26: attempt to index nil with ‘WaitForChild’
--variables
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local manabar = script.Parent:WaitForChild("ManaBar")
local settings = nil
spawn(function() -- make it so the while loop wont yield
while settings == nil do
if not player:FindFirstChild("Settings") then
print ("lily")
else
settings = player.Settings
print ("poo")
end
wait()
end
end)
local PinkBar = script.Parent.ManaBar.PinkBar
--Update the Mana Bar
game:GetService("RunService").RenderStepped:Connect(function()
if settings ~= nil then
local maxmana = settings:WaitForChild("MaxMana") --move these to the loop so it updates every time
local mana = settings:WaitForChild("Mana")
PinkBar.Size = UDim2.new(mana.Value/maxmana.Value, 0, 1, 0)
manabar.ManaStatus.Text = mana.Value.."/"..maxmana.Value
end
end)
No error this time, but the issue persists. Maybe the character picking script will help identify the issue?
local repstorage = game:GetService("ReplicatedStorage")
local remote = repstorage.Remotes.Picking
local function CharPicking(player, ID)
--Terra
if ID == 1 then
local hair = repstorage.Characters.Terra.Hair:Clone()
hair.CFrame = player.Character.Head.CFrame * CFrame.new (0, -0.2, 0.1)
local weld = Instance.new("WeldConstraint")
weld.Parent = hair
weld.Part0 = hair
weld.Part1 = player.Character.Head
hair.Color = Color3.fromRGB(118, 83, 69)
player.Character.Shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=2129197801"
player.Character.Pants.PantsTemplate = "rbxassetid://600064493"
for _, v in ipairs(player.Character:GetChildren()) do
if v:IsA("BasePart") then
v.Color = Color3.fromRGB(255, 206, 180)
end
end
hair.Parent = player.Character
local check = Instance.new("IntValue")
check.Name = "Check"
check.Parent = player
local settings = repstorage.Characters.Terra.Settings:Clone()
if player:FindFirstChild("Settings") then
player.Settings:Destroy()
wait()
end
settings.Parent = player
--Stella
elseif ID == 2 then
local hair = repstorage.Characters.Stella.Hair:Clone()
hair.CFrame = player.Character.Head.CFrame * CFrame.new (0, -0.6, 0.1)
hair.Parent = player.Character
local weld = Instance.new("WeldConstraint")
weld.Parent = hair
weld.Part0 = hair
weld.Part1 = player.Character.Head
player.Character.Shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=4833915541"
player.Character.Pants.PantsTemplate = "http://www.roblox.com/asset/?id=4891915389"
local settings = repstorage.Characters.Stella.Settings:Clone()
if player:FindFirstChild("Settings") then
player.Settings:Destroy()
wait()
end
settings.Parent = player
for _, v in ipairs(player.Character:GetChildren()) do
if v:IsA("BasePart") then
v.Color = Color3.fromRGB(255, 206, 180)
end
end
local check = Instance.new("IntValue")
check.Name = "Check"
check.Parent = player
wait()
end
end
remote.OnServerEvent:Connect(CharPicking)
also the LocalScript for when you press the button to select the character:
local button = script.Parent
local gui = script.Parent.Parent.Parent
local repstorage = game:GetService("ReplicatedStorage")
local remote = repstorage.Remotes.Picking
local ID = 2
local player = game.Players.LocalPlayer
local character = player.Character
button.Activated:Connect(function()
gui.Enabled = false
remote:FireServer(ID)
wait(1)
local health = game.Players.LocalPlayer.PlayerGui:WaitForChild("HealthBarGUI")
local mana = game.Players.LocalPlayer.PlayerGui:WaitForChild("ManaBarGUI")
mana.ManaBar.Visible = true
health.HealthBar.Visible = true
end)
Check and make sure player.Settings is what you want it to be (see if Mana and MaxMana are correct in explorer). also you should mana.Changed Instead of constantly updating the value.
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local manabar = script.Parent:WaitForChild("ManaBar")
local settings = player:WaitForChild('Settings') --Stop everything until this is found.
local maxmana = settings:WaitForChild("MaxMana")
local mana = settings:WaitForChild("Mana")
local PinkBar = script.Parent.ManaBar.PinkBar
--Update the Mana Bar
game:GetService("RunService").RenderStepped:Connect(function()
PinkBar.Size = UDim2.new(mana.Value/maxmana.Value, 0, 1, 0)
manabar.ManaStatus.Text = mana.Value.."/"..maxmana.Value
end)
It worked! I combined your script with some elements of the script sent by @http_shawn and it’s fully functional. I also had to make some changes to other scripts, cause the problem was that the Mana value wasn’t updating according to the newest settings whenever you picked a new character.
current script:
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local manabar = script.Parent:WaitForChild("ManaBar")
local settings = player:WaitForChild('Settings') --Stop everything until this is found.
local PinkBar = script.Parent.ManaBar.PinkBar
--Update the Mana Bar
game:GetService("RunService").RenderStepped:Connect(function()
local maxmana = player:WaitForChild("Settings"):WaitForChild("MaxMana")
local mana = player:WaitForChild("Settings"):WaitForChild("Mana")
PinkBar.Size = UDim2.new(mana.Value/maxmana.Value, 0, 1, 0)
manabar.ManaStatus.Text = mana.Value.."/"..maxmana.Value
end)
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local manabar = script.Parent:WaitForChild("ManaBar")
local settings = player:WaitForChild('Settings') --Stop everything until this is found.
local PinkBar = script.Parent.ManaBar.PinkBar
--Update the Mana Bar
settings:WaitForChild("Mana").Changed:Connect(function()
local maxmana = settings.MaxMana
local mana = settings.Mana
PinkBar.Size = UDim2.new(mana.Value/maxmana.Value, 0, 1, 0)
manabar.ManaStatus.Text = mana.Value.."/"..maxmana.Value
end)