Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

2 comments

0

Thank you. Let me try that (:

0

Thanks. It worked!

This discussion is preserved as it was. New answers, comments, and votes are closed.