Player is nil, yet still prints name?

Hello,

I made a group kill part, but I am getting a weird error.

Script:

function TouchedKill(Hit)
	local Player = Players:GetPlayerFromCharacter(Hit.Parent)
	print(Player.Name)
	local Humanoid = Hit.Parent:FindFirstChild("Humanoid")

	for Groups, _ in ipairs(GoodGroups) do 
		if Player:IsInGroup(GoodGroups[Groups]) then 
			return
		else
			Humanoid.Health = 0 
		end
	end
end

KillPart.Touched:Connect(TouchedKill)

Error:
https://gyazo.com/efb0e16e878b78bbe694811ddebad8eb

Any help would be great.

1 Like

The Touched event was most likely fired for two different parts.

1 Like

Only one:
https://gyazo.com/c3772a5f97ecca148c216a3ef6af2b1c

1 Like

I’ve had this error. To fix it I think you have to put this in a local script?

I was going to reply with the same thing that @N0opimduMb3 replyed with; but I’ll just provide (what should be) code that will silence this error.

For a more indepth explanation, there is probably a small chance that the script detected a part that can’t be found to be brought back as a Player instance, hence the reason it will show as nil and index nil with .Name.

(forgot to remove the not)

function TouchedKill(Hit)
	local Player = Players:GetPlayerFromCharacter(Hit.Parent)
	if Player ~= nil then
		print(Player.Name)
		local Humanoid = Hit.Parent:FindFirstChild("Humanoid")

		for Groups, _ in ipairs(GoodGroups) do 
			if Player:IsInGroup(GoodGroups[Groups]) then 
				return
			else
				Humanoid.Health = 0 
			end
		end
	end
end

KillPart.Touched:Connect(TouchedKill)

Touched events do not work in local scripts afaik + killing the player locally would cause issues.

(Not to mention the ability to just delete the code altogether.)

It works, but now I have another problem:

Error:
https://gyazo.com/aa4c90ae35ee316ba51cfa8fa9119c6c

1 Like

I’m gonna do some testing to try and solve this.

Okay! In the “GoodGroups” table, it is just a bunch of group ids.

1 Like

I tried replicating your variables and got no errors;

This is the code I used.

local KillPart = script.Parent

local Players = game.Players

local GoodGroups = {
	-- put groups in here
	-- ex.
	-- 1,
    -- 2
}

function TouchedKill(Hit)
	local Player = Players:GetPlayerFromCharacter(Hit.Parent)
	if Player ~= nil then
		print(Player.Name)
		local Humanoid = Hit.Parent:FindFirstChild("Humanoid")

		for Groups, _ in ipairs(GoodGroups) do 
			if Player:IsInGroup(GoodGroups[Groups]) then 
				return
			else
				Humanoid.Health = 0 
			end
		end
	end
end

KillPart.Touched:Connect(TouchedKill)
1 Like

Yeah, odd, I just made a completely new script and it seems to have worked. Thank you.

1 Like

Your welcome!

(ghhshhadhshd why is there a limit)

In case anyone wants the rbxl, here it is.

group_touch.rbxl (28.6 KB)