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