Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt to call a nil value fix?

I wanted to make a level progress bar script, but i get the output error, attempt to call a nil value, no line, just the error. My script is

local player = game.Players.LocalPlayer
local leaderboard = player:WaitForChild("leaderstats")
local bar = script.Parent
leaderboard:WaitForChild("xp")
leaderboard:WaitForChild("maxXp")
local maxXp = leaderboard.maxXp.Value
local xp = leaderboard.xp.Value

leaderboard.xp.Changed:Connect()
 bar.Size = UDim2.new((0.165*(xp/maxXp)),0,0,25)

1 answer

✓ Accepted answer

New Code:

local player = game.Players.LocalPlayer
local leaderboard = player:WaitForChild("leaderstats")
local bar = script.Parent
local xp = leaderboard:WaitForChild("xp")
local maxXp = leaderboard:WaitForChild("maxXp")

xp:GetPropertyChangedSignal("Value"):connect(function()
    bar.Size = UDim2.new((0.165*(xp.Value/maxXp.Value)),0,0,25)
end)

The way you had your value variables, they would always have the same value as when you declared them. Now this way when the xp value changes, the bar size will update and you shouldn't get any errors.

This discussion is preserved as it was. New answers, comments, and votes are closed.