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

Why do I sometime get an error message of indexing a nil value when the script has run successfully?

Asked by 5 years ago
Edited 5 years ago

Hello, new coder here!

I'm writing a code for a farm where carrots grow at random intervals, and once fully grown can be harvested for money. It's been working really well for the most part, but every once in a blue moon I get a message saying it attempted to index a nil value, referring to line 44 and the carrot stops growing until the server is restarted. I'm not really sure why it's doing this, but I'm guessing it has something to do with a debounce or some code being called too quickly as this usually happens if I've been standing on top of that carrot for multiple harvests. If this is the issue, I'm not sure where or why I would need a debounce. Any insight on this would be appreciated!

01--Define Starting Variables
02 
03    local stage = 0
04    local GrowthTime = math.random(5,15)
05 
06--Define Growing function
07    function growTheCarrot()
08        if stage == 0 then
09            script.Parent.CFrame = CFrame.new(script.Parent.Position) + Vector3.new(0, -1.5, 0)
10        elseif stage > 0 then
11            script.Parent.CFrame = CFrame.new(script.Parent.Position) + Vector3.new(0, 0.5, 0)
12        end
13    end
14 
15 
View all 48 lines...
0
I don't see why you would get a index error. Try printing player.Parent.Name to gives. sheepposu 561 — 5y
0
also game.Players:GetPlayerFromCharacter(player.Parent) can easily be game.Players[player.Parent.Name] sheepposu 561 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

The reason line 44 may occasionally give you an error is because the part that hit would not actually be a body part of the player. An example would be an accessory you are wearing if that hit the object in question then it would cause it to find the parent of the accessory which since technically the accessory is a part within an accessory it would look for the accessory's name in players. (Bad explanation I am sorry not really sure how else to phrase it).

So there are two ways I can think of to get around this first one check for a humanoid and if its found then run the code or do a pcall()

Check for a humanoid

01script.Parent.Touched:Connect(function(player)
02local humanoid = player.Parent:FindFirstChild("Humanoid")--looks for the players humanoid
03if  humanoid ~= nil then-- if it cannot find it then it hit an accessory or another part meaning nothing will happen if it did find the humanoid a player hit it so it will continue without error
04  if stage == 3 then
05        stage = 0
06        game.Workspace.Swoosh:Play()
07        print ("Carrot Harvested!")
08        growTheCarrot()
09       game.Players[player.Parent.Name].leaderstats.Money.Value = game.Players[player.Parent.Name].leaderstats.Money.Value + 1 -- adding in what sheepposu suggested
10 
11        grow()
12    end
13end
14 
15end)

Another way to go about this is you can use a pcall() function which if an error occurs then the scripts just skips that section and keeps on going and it wont stop the entire script for an error in that section of code

using pcall()

01script.Parent.Touched:Connect(function(player)
02pcall(function()--if an error occurs at any point it just skips on ahead
03    if stage == 3 then
04        stage = 0
05        game.Workspace.Swoosh:Play()
06        print ("Carrot Harvested!")
07        growTheCarrot()
08          game.Players[player.Parent.Name].leaderstats.Money.Value = game.Players[player.Parent.Name].leaderstats.Money.Value + 1 -- adding in what sheepposu suggested
09 
10 
11        grow()
12    end
13end)
14end)

That's all I can think of if this didn't help I am sorry and if any other scripters see I did something wrong please feel free to let me know

0
Thank you! This worked and your explanation helped me get a better understanding of how LUA works. Thanks again! OswinFalls 69 — 5y
Ad

Answer this question