The script I have written below is supposed to create an XP bar for the player that automatically updates as the player's XP changes. However, the XP bar does not update unless my player resets; when it respawns, the bar then updates. Is there anything about my script that could be causing this?
Bar1 = script.Parent.Bar1 PosX1 = Bar1.Position.X.Offset SizeX1 = Bar1.Size.X.Offset Bar2 = script.Parent.Bar2 PosY2 = Bar2.Position.Y.Offset SizeY2 = Bar2.Size.Y.Offset Bar3 = script.Parent.Bar3 PosX3 = Bar3.Position.X.Offset SizeX3 = Bar3.Size.X.Offset Bar4 = script.Parent.Bar4 PosY4 = Bar4.Position.Y.Offset SizeY4 = Bar4.Size.Y.Offset Bar5 = script.Parent.Bar5 PosX5 = Bar5.Position.X.Offset SizeX5 = Bar5.Size.X.Offset xp = script.Parent.Parent.Parent.Parent.leaderstats.StatsStorage.XP level = script.Parent.Parent.Parent.Parent.leaderstats.Level lvlForm = (level.Value*5)^2 frac1 = lvlForm*.125 frac2 = lvlForm*.375 ratio1 = xp.Value/frac1 ratio2 = xp.Value/(frac1+frac2) ratio3 = xp.Value/(frac1+frac2*2) ratio4 = xp.Value/(frac1+frac2*3) ratio5 = xp.Value/lvlForm while true do wait(.01) if level.Value < 100 then if ratio1 < 1 then Bar1.Size = UDim2.new(0, SizeX1*ratio1, 0, 5) Bar1.Position = UDim2.new(0, PosX1+(1-ratio1)/SizeX1, 0, 70) Bar2.Size = UDim2.new(0, 5, 0, 0) Bar2.Position = UDim2.new(0, 70, 0, 0) Bar3.Size = UDim2.new(0, 0, 0, 5) Bar3.Position = UDim2.new(0, 0, 0, 5) Bar4.Size = UDim2.new(0, 5, 0, 0) Bar4.Position = UDim2.new(0, 0, 0, 0) Bar5.Size = UDim2.new(0, 0, 0, 5) Bar5.Position = UDim2.new(0, 0, 0, 70) elseif ratio1 >= 1 and ratio2 < 1 then Bar1.Size = UDim2.new(0, 37, 0, 5) Bar1.Position = UDim2.new(0, 38, 0, 70) Bar2.Size = UDim2.new(0, 5, 0, SizeY2*ratio2) Bar2.Position = UDim2.new(0, 70, 0, PosY2+(1-ratio2)/SizeY2) Bar3.Size = UDim2.new(0, 0, 0, 5) Bar3.Position = UDim2.new(0, 0, 0, 5) Bar4.Size = UDim2.new(0, 5, 0, 0) Bar4.Position = UDim2.new(0, 0, 0, 0) Bar5.Size = UDim2.new(0, 0, 0, 5) Bar5.Position = UDim2.new(0, 0, 0, 70) elseif ratio1 >= 1 and ratio2 >= 1 and ratio3 < 1 then Bar1.Size = UDim2.new(0, 37, 0, 5) Bar1.Position = UDim2.new(0, 38, 0, 70) Bar2.Size = UDim2.new(0, 5, 0, -70) Bar2.Position = UDim2.new(0, 70, 0, 70) Bar3.Size = UDim2.new(0, SizeX3*ratio3, 0, 5) Bar3.Position = UDim2.new(0, PosX3+(1-ratio3)/SizeX3, 0, 0) Bar4.Size = UDim2.new(0, 5, 0, 0) Bar4.Position = UDim2.new(0, 0, 0, 0) Bar5.Size = UDim2.new(0, 0, 0, 5) Bar5.Position = UDim2.new(0, 0, 0, 70) elseif ratio1 >= 1 and ratio2 >= 1 and ratio3 >= 1 and ratio4 < 1 then Bar1.Size = UDim2.new(0, 37, 0, 5) Bar1.Position = UDim2.new(0, 38, 0, 70) Bar2.Size = UDim2.new(0, 5, 0, -70) Bar2.Position = UDim2.new(0, 70, 0, 70) Bar3.Size = UDim2.new(0, -70, 0, 5) Bar3.Position = UDim2.new(0, 70, 0, 0) Bar4.Size = UDim2.new(0, 5, 0, SizeY4*ratio4) Bar4.Position = UDim2.new(0, 0, 0, PosY4+(1-ratio4)/SizeY4) Bar5.Size = UDim2.new(0, 0, 0, 5) Bar5.Position = UDim2.new(0, 0, 0, 70) elseif ratio1 >= 1 and ratio2 >= 1 and ratio3 >= 1 and ratio4 >= 1 and ratio5 < 1 then Bar1.Size = UDim2.new(0, 37, 0, 5) Bar1.Position = UDim2.new(0, 38, 0, 70) Bar2.Size = UDim2.new(0, 5, 0, -70) Bar2.Position = UDim2.new(0, 70, 0, 70) Bar3.Size = UDim2.new(0, -70, 0, 5) Bar3.Position = UDim2.new(0, 70, 0, 0) Bar4.Size = UDim2.new(0, 5, 0, 70) Bar4.Position = UDim2.new(0, 0, 0, 5) Bar5.Size = UDim2.new(0, SizeX5*ratio5, 0, 5) Bar5.Position = UDim2.new(0, PosX5+(1-ratio5)/SizeX5, 0, 70) elseif ratio1 >= 1 and ratio2 >= 1 and ratio3 >= 1 and ratio4 >= 1 and ratio5 >= 1 then Bar1.Size = UDim2.new(0, 37, 0, 5) Bar1.Position = UDim2.new(0, 38, 0, 70) Bar2.Size = UDim2.new(0, 5, 0, -70) Bar2.Position = UDim2.new(0, 70, 0, 70) Bar3.Size = UDim2.new(0, -70, 0, 5) Bar3.Position = UDim2.new(0, 70, 0, 0) Bar4.Size = UDim2.new(0, 5, 0, 70) Bar4.Position = UDim2.new(0, 0, 0, 5) Bar5.Size = UDim2.new(0, 38, 0, 5) Bar5.Position = UDim2.new(0, 5, 0, 70) elseif level.Value >= 100 then Bar1.Visible = true Bar1.Size = UDim2.new(0, 37, 0, 5) Bar1.Position = UDim2.new(0, 38, 0, 70) Bar2.Visible = true Bar2.Size = UDim2.new(0, 5, 0, -70) Bar2.Position = UDim2.new(0, 70, 0, 70) Bar3.Visible = true Bar3.Size = UDim2.new(0, -70, 0, 5) Bar3.Position = UDim2.new(0, 70, 0, 0) Bar4.Visible = true Bar4.Size = UDim2.new(0, 5, 0, 70) Bar4.Position = UDim2.new(0, 0, 0, 5) Bar5.Visible = true Bar5.Size = UDim2.new(0, 38, 0, 5) Bar5.Position = UDim2.new(0, 5, 0, 70) end end end
Bar1 = script.Parent:WaitForChild("Bar1") PosX1 = Bar1.Position.X.Offset SizeX1 = Bar1.Size.X.Offset Bar2 = script.Parent:WaitForChild("Bar2") PosY2 = Bar2.Position.Y.Offset SizeY2 = Bar2.Size.Y.Offset Bar3 = script.Parent:WaitForChild("Bar3") PosX3 = Bar3.Position.X.Offset SizeX3 = Bar3.Size.X.Offset Bar4 = script.Parent:WaitForChild("Bar4") PosY4 = Bar4.Position.Y.Offset SizeY4 = Bar4.Size.Y.Offset Bar5 = script.Parent:WaitForChild("Bar5") PosX5 = Bar5.Position.X.Offset SizeX5 = Bar5.Size.X.Offset xp = script.Parent.Parent.Parent.Parent:WaitForChild(""leaderstats"):WaitForChild("StatsStorage.XP") level = script.Parent.Parent.Parent.Parent:WaitForChild(""leaderstats"):WaitForChild("Level") lvlForm = (level.Value*5)^2 frac1 = lvlForm*.125 frac2 = lvlForm*.375 ratio1 = xp.Value/frac1 ratio2 = xp.Value/(frac1+frac2) ratio3 = xp.Value/(frac1+frac2*2) ratio4 = xp.Value/(frac1+frac2*3) ratio5 = xp.Value/lvlForm while true do wait(.01) if level.Value < 100 then if ratio1 < 1 then Bar1.Size = UDim2.new(0, SizeX1*ratio1, 0, 5) Bar1.Position = UDim2.new(0, PosX1+(1-ratio1)/SizeX1, 0, 70) Bar2.Size = UDim2.new(0, 5, 0, 0) Bar2.Position = UDim2.new(0, 70, 0, 0) Bar3.Size = UDim2.new(0, 0, 0, 5) Bar3.Position = UDim2.new(0, 0, 0, 5) Bar4.Size = UDim2.new(0, 5, 0, 0) Bar4.Position = UDim2.new(0, 0, 0, 0) Bar5.Size = UDim2.new(0, 0, 0, 5) Bar5.Position = UDim2.new(0, 0, 0, 70) elseif ratio1 >= 1 and ratio2 < 1 then Bar1.Size = UDim2.new(0, 37, 0, 5) Bar1.Position = UDim2.new(0, 38, 0, 70) Bar2.Size = UDim2.new(0, 5, 0, SizeY2*ratio2) Bar2.Position = UDim2.new(0, 70, 0, PosY2+(1-ratio2)/SizeY2) Bar3.Size = UDim2.new(0, 0, 0, 5) Bar3.Position = UDim2.new(0, 0, 0, 5) Bar4.Size = UDim2.new(0, 5, 0, 0) Bar4.Position = UDim2.new(0, 0, 0, 0) Bar5.Size = UDim2.new(0, 0, 0, 5) Bar5.Position = UDim2.new(0, 0, 0, 70) elseif ratio1 >= 1 and ratio2 >= 1 and ratio3 < 1 then Bar1.Size = UDim2.new(0, 37, 0, 5) Bar1.Position = UDim2.new(0, 38, 0, 70) Bar2.Size = UDim2.new(0, 5, 0, -70) Bar2.Position = UDim2.new(0, 70, 0, 70) Bar3.Size = UDim2.new(0, SizeX3*ratio3, 0, 5) Bar3.Position = UDim2.new(0, PosX3+(1-ratio3)/SizeX3, 0, 0) Bar4.Size = UDim2.new(0, 5, 0, 0) Bar4.Position = UDim2.new(0, 0, 0, 0) Bar5.Size = UDim2.new(0, 0, 0, 5) Bar5.Position = UDim2.new(0, 0, 0, 70) elseif ratio1 >= 1 and ratio2 >= 1 and ratio3 >= 1 and ratio4 < 1 then Bar1.Size = UDim2.new(0, 37, 0, 5) Bar1.Position = UDim2.new(0, 38, 0, 70) Bar2.Size = UDim2.new(0, 5, 0, -70) Bar2.Position = UDim2.new(0, 70, 0, 70) Bar3.Size = UDim2.new(0, -70, 0, 5) Bar3.Position = UDim2.new(0, 70, 0, 0) Bar4.Size = UDim2.new(0, 5, 0, SizeY4*ratio4) Bar4.Position = UDim2.new(0, 0, 0, PosY4+(1-ratio4)/SizeY4) Bar5.Size = UDim2.new(0, 0, 0, 5) Bar5.Position = UDim2.new(0, 0, 0, 70) elseif ratio1 >= 1 and ratio2 >= 1 and ratio3 >= 1 and ratio4 >= 1 and ratio5 < 1 then Bar1.Size = UDim2.new(0, 37, 0, 5) Bar1.Position = UDim2.new(0, 38, 0, 70) Bar2.Size = UDim2.new(0, 5, 0, -70) Bar2.Position = UDim2.new(0, 70, 0, 70) Bar3.Size = UDim2.new(0, -70, 0, 5) Bar3.Position = UDim2.new(0, 70, 0, 0) Bar4.Size = UDim2.new(0, 5, 0, 70) Bar4.Position = UDim2.new(0, 0, 0, 5) Bar5.Size = UDim2.new(0, SizeX5*ratio5, 0, 5) Bar5.Position = UDim2.new(0, PosX5+(1-ratio5)/SizeX5, 0, 70) elseif ratio1 >= 1 and ratio2 >= 1 and ratio3 >= 1 and ratio4 >= 1 and ratio5 >= 1 then Bar1.Size = UDim2.new(0, 37, 0, 5) Bar1.Position = UDim2.new(0, 38, 0, 70) Bar2.Size = UDim2.new(0, 5, 0, -70) Bar2.Position = UDim2.new(0, 70, 0, 70) Bar3.Size = UDim2.new(0, -70, 0, 5) Bar3.Position = UDim2.new(0, 70, 0, 0) Bar4.Size = UDim2.new(0, 5, 0, 70) Bar4.Position = UDim2.new(0, 0, 0, 5) Bar5.Size = UDim2.new(0, 38, 0, 5) Bar5.Position = UDim2.new(0, 5, 0, 70) elseif level.Value >= 100 then Bar1.Visible = true Bar1.Size = UDim2.new(0, 37, 0, 5) Bar1.Position = UDim2.new(0, 38, 0, 70) Bar2.Visible = true Bar2.Size = UDim2.new(0, 5, 0, -70) Bar2.Position = UDim2.new(0, 70, 0, 70) Bar3.Visible = true Bar3.Size = UDim2.new(0, -70, 0, 5) Bar3.Position = UDim2.new(0, 70, 0, 0) Bar4.Visible = true Bar4.Size = UDim2.new(0, 5, 0, 70) Bar4.Position = UDim2.new(0, 0, 0, 5) Bar5.Visible = true Bar5.Size = UDim2.new(0, 38, 0, 5) Bar5.Position = UDim2.new(0, 5, 0, 70) end end end
Try this, I added [:WaitForChild("")] so when the player loads the script doesn't start without loading the items. Hope this works! I probably didn't explain this properly, so I apologize for that.