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
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
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)```
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!
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)
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.