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!


--Define Starting Variables local stage = 0 local GrowthTime = math.random(5,15) --Define Growing function function growTheCarrot() if stage == 0 then script.Parent.CFrame = CFrame.new(script.Parent.Position) + Vector3.new(0, -1.5, 0) elseif stage > 0 then script.Parent.CFrame = CFrame.new(script.Parent.Position) + Vector3.new(0, 0.5, 0) end end --Call Grow function and check if ready function grow() repeat wait (GrowthTime) stage = stage +1 growTheCarrot() print ("The carrot has grown to Stage "..stage) until stage == 3 end --Call for Carrot to grow growTheCarrot() grow() --Add money to player and regrow script.Parent.Touched:Connect(function(player) if stage == 3 then stage = 0 game.Workspace.Swoosh:Play() print ("Carrot Harvested!") growTheCarrot() game.Players:GetPlayerFromCharacter(player.Parent).leaderstats.Money.Value = game.Players:GetPlayerFromCharacter(player.Parent).leaderstats.Money.Value + 1 grow() end end)
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

script.Parent.Touched:Connect(function(player)
local humanoid = player.Parent:FindFirstChild("Humanoid")--looks for the players humanoid
if  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
  if stage == 3 then
        stage = 0
        game.Workspace.Swoosh:Play()
        print ("Carrot Harvested!")
        growTheCarrot()
       game.Players[player.Parent.Name].leaderstats.Money.Value = game.Players[player.Parent.Name].leaderstats.Money.Value + 1 -- adding in what sheepposu suggested

        grow()
    end
end 

end)

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()

script.Parent.Touched:Connect(function(player)
pcall(function()--if an error occurs at any point it just skips on ahead 
    if stage == 3 then
        stage = 0
        game.Workspace.Swoosh:Play()
        print ("Carrot Harvested!")
        growTheCarrot()
          game.Players[player.Parent.Name].leaderstats.Money.Value = game.Players[player.Parent.Name].leaderstats.Money.Value + 1 -- adding in what sheepposu suggested


        grow()
    end
end)
end)

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