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

attempt to index local 'Player' (a nil value) error? How to fix

Asked by 6 years ago

Hello there I have a problem with a touch it should take the backpack value and add it to the leaderstats Value and then comes the error in the console how do I fix that here is my script:

local toggle = 10
script.Parent.Parent.Touched:Connect(function(hit)
    if toggle == 10 then
        toggle = 0
        local playern = hit.Parent.Name
        local Player = game.Players:FindFirstChild(playern)
        print("found")
        local Save = Player.BackpackFill.Value 
        Player.leaderstats.Tea.Value = Save
        Player.BackpackFill.Value = 0 
        wait(2)
        toggle = 10
    end
end)

2 answers

Log in to vote
0
Answered by
AIphanium 124
6 years ago
Edited 6 years ago

Hello, Lazytfft12.

I believe the problem is, your script is not checking if the (hit) has a Parent or a Humanoid.

I have edited your script.

local toggle = 10
script.Parent.Parent.Touched:Connect(function(hit)
    if toggle == 10 then
       if hit.Parent and hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid") then

        toggle = 0
        local playern = hit.Parent.Name
        local playern2 = hit.Parent.Parent.Name 

        if game.Players:FindFirstChild(playern) or game.Players:FindFirstChild(playern2) then
        print("Player Detected")
        local Save = Player.BackpackFill.Value 
        Player.leaderstats.Tea.Value = Save
        Player.BackpackFill.Value = 0 
        wait(2)
        toggle = 10
   else
print("No Player Found")
 return
end
      else
          return
   end
    end
wait(0.5)
end)
0
What? He's checking if an object with the name of whatever hit.Parent is is in Players, User#24403 69 — 6y
0
There are many ways of finding a PLAYER from their character, and this is my way of finding a PLAYER. AIphanium 124 — 6y
0
But you're doing 2 checks, when just one is needed. See my example User#24403 69 — 6y
0
As i said, this is MY way of finding a PLAYER, oh and also, changing the amount of checks (if's) just makes the script more accurate. AIphanium 124 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

The reason for this is because when FindFirstChild cannot find an object with the name specified, it will return nil. This is what happened in your case, since an object with the name hit.Parent.Name was not found. You need a check to see if the player exists

local toggle = 10
script.Parent.Parent.Touched:Connect(function(hit)
    if toggle == 10 then
        toggle = 0
        local char = hit.Parent
        local Player = game.Players:GetPlayerFromCharacter(char)

        if Player then -- Checking if it was an actual player.
            print("found")
            local Save = Player.BackpackFill.Value 
            Player.leaderstats.Tea.Value = Save
            Player.BackpackFill.Value = 0 
            wait(2)
            toggle = 10
        end
    end
end)

As you can see I used a different method of getting the player. I used GetPlayerFromCharacter. It takes a model as an argument. If a player's character is the model passed, it will return the player from that character. It's a bit more reliable than FindFirstChild.


Hopefully this answered your question and if it did, then don't forget to hit that accept button. If you have any more questions then feel free to leave them down in the comments.
0
FindFirstChild() is useless in this situation, use WaitForChild() DeceptiveCaster 3761 — 6y
0
No. It is not. User#24403 69 — 6y
0
FindFirstChild() is useful, but MCAndRobloxUnited is right, WaitForChild() is a great way of waiting until the part is touched, it also reduces lag from the parts that move alot. AIphanium 124 — 6y
0
did you really have to downvote me? WaitForChild would yield the script if a humanoid wasn't found! User#24403 69 — 6y
View all comments (5 more)
0
I didn't downvote you :l AIphanium 124 — 6y
0
WaitForChild may stop the script until something is found, but there most be a way to activate WaitForChild() when the part is touched. AIphanium 124 — 6y
0
I respect your answer, but you most be more accurate... AIphanium 124 — 6y
0
:FindFirstChild() is intended for checking if a object exists User#23365 30 — 6y
0
I know, but we were talking about "WaitForChild()" AIphanium 124 — 6y

Answer this question