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

How can I prevent this from happening?

Asked by 10 years ago

I have a script that needs to get data from a model in ServerStorage but it keeps giving me this error: attempt to index a nil value

The script line is this:

for i,v in pairs(game:GetService("ServerStorage"):FindFirstChild("Data"):GetChildren()) do

Then it gets the data from it but sometimes it has nothing in it and it checks it any way returning a nil value and therefore returns an error. Do YOU know how to fix this?

3 answers

Log in to vote
0
Answered by 10 years ago

Try:

Model =  game.ServerStorage:FindFirstChild("Data")
 for i, v in pairs (Model:GetChildren()) do
 end

**I think the problem was ServerStorage doesnt work with GetService. Not 100% sure.

Ad
Log in to vote
0
Answered by 10 years ago

if v then ... end

perhaps???

Log in to vote
0
Answered by 10 years ago

I think the problem here is that you don't have a statement that checks if Data exists. If Data doesn't exist but the script runs this line anyways, it will error because the script can't find Data! A way to fix this is to add an if statement checking if Data exists before running the code.

if game:GetService("ServerStorage"):FindFirstChild("Data") ~= nil then
    for i,v in pairs(game:GetService("ServerStorage"):FindFirstChild("Data"):GetChildren()) do

I hope this helps!

Answer this question