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)" while it's not nil?

Asked by 5 years ago
Edited 5 years ago

Hello, I made a script opening a door if the player touching it has a certain rank in a group, which works, but the part getting a player from character does not seem to work (after the rank detection, the script continue but I've choose not to include it). Here, hit is the character touching the door.

 function Move(Hit)
    local Character = Hit.Parent
    local Player = game.Players:GetPlayerFromCharacter(Character)
    if Player == nil then
        print("Nil.")
    end 
    print("Door " .. script.Parent.Name .. " detected player " .. Player.Name .. ", checking rank.")
    if Player:GetRankInGroup(4274321) >= 251 and Player:IsInGroup(4274321) then

The output is this:

Nil.
  10:23:49.829 - Workspace.GroupLightDoor1.Script:21: attempt to index local 'Player' (a nil value)
10:23:49.830 - Stack Begin
10:23:49.830 - Script 'Workspace.GroupLightDoor1.Script', Line 21
10:23:49.830 - Stack End
  Door GroupLightDoor1 detected player CreatorMan2006, checking rank.

When I play the game in studio, and walks to the door, it opens, but errors "attempt to index local 'Player' (a nil value)". From what I know, nil is something that does not exist. If the player was nil, the door shouldn't be able to open since it could not detect the player's rank. This error does not ruin the game, but each time the door opens, it throws the error, and I don't know how to get rid of it.

0
you should add return under the statement print("Nil."), otherwise the script is going to continue through your Player manipulations even though `Player` is nil Vulkarin 581 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Try adding these checks. This checks if the part that touched the door is a player and isn't nil.

if Player ~= nil and hit.Parent:FindFirstChild("Humanoid") then
--Rest of the code
end
0
It worked, thanks! :D CreatorMan2006 -2 — 5y
0
No problem wilsonsilva007 373 — 5y
Ad

Answer this question