Leaderstats keeps giving an error with "attempt to index nil"

I’m wanting to make a small arcade game that checks the player’s chip count, and if it is enough, let them play the machine, but I keep getting an error with attempting to index a nil value. I did try searching on the forum under ‘how to check if a player has enough value to buy something’, but I didn’t see anything that was helpful. Here is my code as it stands atm:

local CardReader = game.Workspace["Quik Drop"].CardReader
local CardReaderText = CardReader.Cardslot.SurfaceGui.TextLabel
local DeadZone = game.Workspace["Quik Drop"].DeadZone
local Button = game.Workspace["Quik Drop"].Button
local GameboardText = game.Workspace["Quik Drop"].Gameboard.Gameboard.SurfaceGui.TextLabel
local JackpotDisplay = game.Workspace["Quik Drop"].Jackpot.JackpotDisplay.SurfaceGui.TextLabel
local Balls = game.Workspace["Quik Drop"]["Balls"]
local Chips = game.Players.LocalPlayer.leaderstats.Chips.value


CardReader.ClickDetector.MouseClick:Connect(function(player)
	CardReaderText.Text = "Checking Balance..."
	wait(.2)
	if Chips >= 5 then
		CardReaderText.Text = "Enjoy your game!"
		wait(.2)
		CardReaderText.Text = "#Chips"
	end
end)

Update: I decided to put everything in 1 script instead of across multiple scripts

1 Like

I tried this, and it still says attempt to index nil, apologies for late reply

1 Like

You forgot to capitalise value.

If this is somehow not the problem, then dunno.

2 Likes

local Chips = game.Players.LocalPlayer.leaderstats.Chips.value`

in your variables, you made a mistake in the “Value” section you typed the letter V in lowercase, and also you needed WaitForChild to wait for everything to load properly.
These are the correct variables:

local Players = game:GetService("Players").LocalPlayer
local leaderstats = Players:WaitForChild("leaderstats")
local Chips = leaderstats:WaitForChild("Chips").Value

I get attempt to index nil when I try this

1 Like

Well This what you are looking for :

local CardReader = game.Workspace["Quik Drop"].CardReader
local CardReaderText = CardReader.Cardslot.SurfaceGui.TextLabel
local DeadZone = game.Workspace["Quik Drop"].DeadZone
local Button = game.Workspace["Quik Drop"].Button
local GameboardText = game.Workspace["Quik Drop"].Gameboard.Gameboard.SurfaceGui.TextLabel
local JackpotDisplay = game.Workspace["Quik Drop"].Jackpot.JackpotDisplay.SurfaceGui.TextLabel
local Balls = game.Workspace["Quik Drop"]["Balls"]

CardReader.ClickDetector.MouseClick:Connect(function(player) <-- Player
    local Chips = player.leaderstats.Chips.value
	CardReaderText.Text = "Checking Balance..."
	wait(.2)
	if Chips >= 5 then
		CardReaderText.Text = "Enjoy your game!"
		wait(.2)
		CardReaderText.Text = "#Chips"
	end
end)

I already have that in my code, I changed it last night, this is what it’s at atm:

local leaderstats = Players:Joined("leaderstats")
local Chips = leaderstats:WaitForChild("Chips").Value
local CardReader = game.Workspace["Quik Drop"].CardReader
local CardReaderText = CardReader.Cardslot.SurfaceGui.TextLabel
local DeadZone = game.Workspace["Quik Drop"].DeadZone
local Button = game.Workspace["Quik Drop"].Button
local GameboardText = game.Workspace["Quik Drop"].GameBoard.Gameboard.SurfaceGui.TextLabel
local JackpotDisplay = game.Workspace["Quik Drop"].Jackpot.JackpotDisplay.SurfaceGui.TextLabel
local Balls = game.ServerStorage.Balls
local IsPlaying = false
local Timer = 30
local Jackpot = 500
AmountDropped = 0
TouchedJar = 0
local BallTouchedJar = game.Workspace["Quik Drop"].RotatingPlatforms.Jug.Dropzone.BallDetectJar
function StartGame(hit)
	JackpotDisplay.Text = Jackpot
	CardReader.Cardslot.ClickDetector.MouseClick:Connect(function(player)
		CardReaderText.Text = "Checking Balance..."
		wait(1)
		if IsPlaying == false and Chips >= 5 then
			CardReaderText.Text = "Enjoy your game!"
			wait(2)
			CardReaderText.Text = "Game in progress"
			IsPlaying = true
			else
			CardReaderText.Text = "Not enough Chips!"
			end
		end)
		GameboardText.Text = "Welcome to Quik Drop!"
		wait(4)
		GameboardText.Text = "The goal is to drop all 50 balls into the bucket before the time runs out."
		wait(4)
		GameboardText.Text= "If you drop all 50 balls into the bucket, you will win the Jackpot!"
		wait(4)
		GameboardText.Text = "Good luck!"
		wait(4)
	repeat
		GameboardText.Text = Timer
		wait(1)
		GameboardText.Text = Timer - 1
	until Timer == 0
	end
StartGame()
Button.button.ClickDetector.MouseClick:Connect(function(hit)
	local ballcopy = Balls:Clone()
	ballcopy.Parent = game.Workspace
	Balls.Ball1.Anchored = false
	AmountDropped = AmountDropped + 1
	if AmountDropped >= 50 or Timer == 0 then
		Balls:Destroy()
		Balls.Parent = game.ServerStorage
		GameboardText = "All balls have been dropped! Let's see how you did!"
		if TouchedJar <= 49 then
			GameboardText.Text = "It looks like you did not quite get all 50 balls."
			wait(2)
			GameboardText.Text = "You got" .. #TouchedJar "balls in the Jar."
			wait(2)
			GameboardText.Text = "Try again next time!"
		elseif TouchedJar >= 50 then
			GameboardText.Text = "You've hit the jackpot of" .. #Jackpot "! Congratulations!"
			
			
			AmountDropped = 0
			TouchedJar = 0
			
		end
		
		
		
	end
end)```
1 Like

Is this a local script or a server script?
If it is a server script then game.Players.LocalPlayer will not work


Additionally I see an error regarding the way you defined chips

local Chips = game.Players.LocalPlayer.leaderstats.Chips.value

By doing this you are defining chips at that current value, here’s what I mean if you don’t understand

script.Parent.IntValue.Value = 10
local Chips = script.Parent.IntValue.Value -- Chips = 10
script.Parent.IntValue.Value = 100
print(Chips) -- Print's 10

So I would change that to defining the instance

local Chips = script.Parent.IntValue
print(Chips.Value)

Every single object you try to access needs a :WaitForChild before accessing it for the first time.

For example:

local JackpotDisplay = game.Workspace:WaitForChild("Quik Drop"):WaitForChild("Jackpot"):WaitForChild("JackpotDisplay"):WaitForChild("SurfaceGui"):WaitForChild("TextLabel")

Edit: We now know it’s not a local script, so what I said above is not necessarily true. Definitely true for local scripts however!

1 Like

It’s just a regular script inside of the machine
image

Alright so as I just said you need to not use local player

Instead use the player instance that the MouseClick function gives you


CardReader.ClickDetector.MouseClick:Connect(function(player)
    local Chips = player.leaderstats.Chips.value -- Right here
	CardReaderText.Text = "Checking Balance..."
	wait(.2)
	if Chips >= 5 then
		CardReaderText.Text = "Enjoy your game!"
		wait(.2)
		CardReaderText.Text = "#Chips"
	end
end)
2 Likes

The script isn’t inside the IntValue though, wouldn’t that just cause it to say it’s not a valid member of Quik drop?

Is this script a server script? If so, then server can’t see the localplayer

Could you please try what people’ve been alerting you about the code?

I already mentioned that it is a normal script inside of the machine

I was using that as an example

Now when I try it, it doesn’t do anything, nothing in output as well

Please, colaborate to us. It almost seems like you want to be spoonfed. And, that’s, what I’m gonna do:

local Quik_Drop = game:GetService("Workspace"):FindFirstChild('Quik Drop')

local CardReader = Quik_Drop.CardReader
local CardReaderText = CardReader.Cardslot.SurfaceGui.TextLabel
local DeadZone = Quik_Drop.DeadZone
local Button = Quik_Drop.Button
local GameboardText = Quik_Drop.Gameboard.Gameboard.SurfaceGui.TextLabel
local JackpotDisplay = Quik_Drop.Jackpot.JackpotDisplay.SurfaceGui.TextLabel
local Balls = Quik_Drop["Balls"]

CardReader.ClickDetector.MouseClick:Connect(function(player)
	local Chips = player:WaitForChild('Chips').Value
	
	CardReaderText.Text = "Checking Balance..."
	wait(.2)
	if Chips >= 5 then
		CardReaderText.Text = "Enjoy your game!"
		wait(.2)
		CardReaderText.Text = Chips.."Chips"
	end
end)

I’ve already tried what kinger put in his post, and the script already seems to be fine finding the quik drop machine on its own when I put game.workspace.quikdrop without waitforchild. I was just replying saying that it doesn’t work whenever I click on the card reader.