Trouble with getting rid of Error

What I was trying to build was a gui that pops up and shows the ammo that the gun has. The script works perfectly, but for some reason there’s an error that’s “Bugging” me, and I don’t know how to get rid of it.

The scripts’s job is the find a ceratin tool in its Inventory, and the Gui is supposed to say the NumberValue found in the tool. Here’s the Error:

Script:

local player = game.Players.LocalPlayer
local Char = player.Character

while wait() do
	script.Parent.Text = Char:WaitForChild("Pistol").Configuration.CurrentBullets.Value
end
1 Like

Weird this error should only appear when you used WaitForChild. Did you use it anywhere?
Edit:He scammed me by not putting WaitForChild in the code so i said that.

Yea, I already tried that method

Well i guess i can’t help with this.
But i just want to say you can change the text when the ammos value changed instead of looping. That can make the game less lagging (If it does)

If you replace

while wait() do

with

while wait() and player.Character do

does it fix it?

old response

Try replacing

local Char = player.Character

with

local Char = player.Character or player.CharacterAdded:Wait()

It didn’t work, I guess i’ll just live with it.

Instead of Char:WaitForChild(), use Plr.Backpack:FindFirstChild(). Also, change the second line of your script into local Char = Plr.Character or Plr.CharacterAdded:Wait(). This will make it so that it will wait for the players character to load so it won’t turn nil.

it completely brok the script.

Sorry, but I assume that it’s a local script (The script that you’ve prompted that has the error) inside of a TextLabel, am I correct?

local Char = player.Character or player.CharacterAdded:Wait()

Do this instead.

it seems like Char seems to be nil you shoudl replace the 2nd line to
local Char = Player.Character or Player.CharacterAdded:Wait()

You mention that

The script works perfectly,

So does the code you included in your post work correctly and randomly errors, without causing any issues?

If it doesn’t work correctly ever, refer to the “old response” section of my original reply.