Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is "player" a nil value?

There is a Workspace.Part.Script:5: attempt to index local 'player' (a nil value) poping up in the output - can anyone explain it so i can understand - goku

local part = script.Parent

part.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player:IsInGroup(2793019) then
		print("eeeeeeeeeeeeeeeee")
	end
	
end)

1 comment

0

GetPlayerFromCharacter will return nil if it cannot get the player from the character model.

2 answers

✓ Accepted answer

You are not checking if what Touched is even Part of a Character. To do so check if hit.Parent has a HumanoidRootPart.

Try this;

local part = script.Parent

part.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent or hit.Parent.Parent)
    if player and player:IsInGroup(2793019) then
        print("eeeeeeeeeeeeeeeee")
    end
end)

I believe it's because the part is touching an accessory or some other child of a part within the Character player model. If that's hard to understand let me know, I'm not good at explaining.

1 comment

0

This won't work because of how or works. Or only chooses the second one if the first one is nil, and if there's a hit.Parent.Parent of course hit.Parent isn't nil.

This discussion is preserved as it was. New answers, comments, and votes are closed.