Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I fix "Attempt to index nil with Value" in my game?

Asked by 5 years ago

I was making a game from a tutorial and I copied and checked everything exactly as it should have been. However, when I played the game, this error message appeared:

01ServerScriptService.Script:17: attempt to index nil with 'Value'
02 
03It said that the problem was this line:
04return game.ServerStorage.Cars:FindFirstChild(NameOfCar.Price.Value)
05This is the code that was inside the script.
06Does anyone know how to fix this?
07 
08 
09game.Players.PlayerAdded:Connect(function(plr)     
10local leaderstats = Instance.new("Folder")
11local folder = leaderstats
12leaderstats.Name = "leaderstats"   
13leaderstats.Parent = plr
14local cash = Instance.new("IntValue")
15cash.Name = "Cash"
View all 34 lines...

1 answer

Log in to vote
1
Answered by 5 years ago

When you code NameOfCar.Price.Value, you are indexing NameOfCar to find Price, and then Price to find value. However, the error say you are attempting to index a Nil value with Value, which would indicate that Price is nil.

I suspect, however, that what you meant to write was:

1return game.ServerStorage.Cars:FindFirstChild(NameOfCar).Price.Value

The error resulted from you Indexing NameOfCar.Price, which does not exist because NameOfCar is a string. You have to close the parentheses on FindFirstChild, so that the function can find the Car model, which is what contains the Price variable.

0
Thank you. Let me try that (: repplayer 2 — 5y
0
Thanks. It worked! repplayer 2 — 5y
Ad

Answer this question