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 4 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:

ServerScriptService.Script:17: attempt to index nil with 'Value'

It said that the problem was this line:
return game.ServerStorage.Cars:FindFirstChild(NameOfCar.Price.Value)
This is the code that was inside the script. 
Does anyone know how to fix this?


game.Players.PlayerAdded:Connect(function(plr)      
local leaderstats = Instance.new("Folder")
local folder = leaderstats
leaderstats.Name = "leaderstats"    
leaderstats.Parent = plr
local cash = Instance.new("IntValue")
cash.Name = "Cash"
local currencyName = cash.Name
cash.Value = 40000
cash.Parent = leaderstats
end)

game.ReplicatedStorage:WaitForChild("CheckPrice").OnServerInvoke = function(player,NameOfCar)


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

game.ReplicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player, NameOfCar)
    local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
    car:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0,0,15))
    car.Parent = workspace
    car:MakeJoints()
    car.Name = player.Name.."'s ".."NameOfCar"
end)

1 answer

Log in to vote
1
Answered by 4 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:

return 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 — 4y
0
Thanks. It worked! repplayer 2 — 4y
Ad

Answer this question