Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make an XP bar?

Asked by 8 years ago

Why does this XP bar not working?

script.Parent.Parent.Parent.Parent.Parent.leaderstats.XOP.Value I tried didn't work so i had to find a other why to get the stats.

local user = game.Players:GetPlayers()
local stats = user:findFirstChild("leaderstats")
xp = stats:findFirstChild("XOP")
lvl = stats:findFirstChild("Level")

while true do
wait()
local FR = ( xp.Value / (lvl.Value * 100))
script.Parent.Size = UDim2.new( FR , 0 , 0, 10)
end

Error: Script 'Players.Player.PlayerGui.XPGui.Frame.Fill.Script', Line 2 and Players.Player.PlayerGui.XPGui.Frame.Fill.Script:2: attempt to call method 'findFirstChild' (a nil value)

0
user is a table, which means it doesn't have a FindFirstChild. User#6546 35 — 8y
0
Is their another way to find leaderstats? minetrackmania 186 — 8y

1 answer

Log in to vote
0
Answered by
DevChris 235 Moderation Voter
8 years ago

I tidied your code up a bit. If it didn't work, make sure all your values exist.

local user = game.Players.LocalPlayer -- :GetPlayers() gets all the players and returns a table. Shouldn't really be used at all in localscripts. You only need the local player.
local stats = user:findFirstChild("leaderstats")
xp = stats:findFirstChild("XOP")
lvl = stats:findFirstChild("Level")

local function changed() -- Use events rather than a loop for this type of thing.
    local FR = (xp.Value / (lvl.Value * 100))
    script.Parent.Size = UDim2.new(FR, 0, 0, 10)
end

xp.Changed:connect(changed)
lvl.Changed:connect(changed)
0
You did answer on my other question correct you need change findFirstChild to WaitForChild may edit it in here and i will give accept answer. ;) minetrackmania 186 — 8y
Ad

Answer this question