Leaderstats Index Problem

  1. I want to achieve a script where gui buttons become invisible after clicking on a button. That is easy, the problem is I want to make the button be an in-game currency purchase, that costs 10 Tickets.

  2. For some reason, it can’t get the leaderstats from the player. I tried so many youtube videos and a little old forums about it… I can’t find an answer. So I come here to get one directly myself.

Yes, the value is named leaderstats, yes the leaderboard works and everything works fine.

This is the error that shows in the output.
Players.SuperBirth1.PlayerGui.ColaLaunchGUI.Frame.TextButton1.Script:6: attempt to index nil with 'leaderstats’

Here is the code.
All the “locals” below Player are the gui stuff that should become invisible.

local Player = game.Players.LocalPlayer
local button1 = script.Parent
local button2 = script.Parent.Parent.TextButton2
local label = script.Parent.Parent.TextLabelwhere
script.Parent.MouseButton1Click:Connect(function()
	if Player.leaderstats.Tickets.Value >= 10 then
	print("oh wow you have enough money!")
	Player.leaderstats.Tickets.Value = Player.leaderstats.Tickets.Value - 10
	button1.Visible = false
	button2.Visible = false
	label.Visible = false
	else
        print("loser")
end
end)

add :WaitForChild(“leaderstats”) instead of leaderstats.

Still doesn’t work. I tried it earlier too

Hmm, lemme think and i’ll come up with a new solution that may be the one.

1 Like

do:

if Player:WaitForChild("leaderstats"):WaitForChild("Tickets").Value then
...
end

if that doesnt work try:

for i,v in pairs(Player) do
print(i, v.Name)
end

send screenshot if youre not sure what to do!

I told him earlier to use waiftorchild, but it doesn’t seem to wrok, hopefully your second option is the solution :pray:

yeah, its just a check to see what objects it has in the player

I’m sorry, I’m not experienced with the “for i,v in pairs” stuff.
image
Will you please explain where I should put it?

Try the code first, and if it doesnt work he wil ltell you .

just paste it below “label” and take a screenshot of the output

sorry forgot to say:
for i,v in pairs(Player:GetChildren()) do
print(i, v.Name)
end

Ok the problem could be that you are not using a local script

1 Like

Bro… I literally.

This is what happens when you type script in way too fast and at night with barely any energy to spare.

Thank you both.

However, I swear I tried using a LocalScript earlier! And it didn’t work… I’m so confused.

1 Like

The entire reason why I wasn’t using LocalScript was because I tried it earlier and it didn’t work, but now that you made me try it again; it works. I might have done something wrong when doing the local script. (initially)