GM:PlayerShouldTakeDamage from world

I am currently working on this new gamemode for GMod and I have run into a problem. There is a map with poison water that is creating errors for my GM:ShouldTakeDamage event. I know its something stupid but I can’t find out what it is. Here is the code


function GM:PlayerShouldTakeDamage( victim, attacker )

	if attacker:IsWorld() then return true end
	if attacker:IsNPC() then return true end
	if (attacker:GetUserGroup() == victim:GetUserGroup()) then return false end
	if (attacker:GetUserGroup() ~= victim:GetUserGroup()) then return true end
end

Error created: attempt to call method ‘GetUserGroup’ (a nil value)

Keep in mind this code does work with players, zombies, and the world, but it creates errors only with the poison water thing.

Probably because it’s an entity hurting it, not the world and it’s no NPC, also it’s not a player so it doesn’t hold the GetUserGroup method. You need to add another if statement to check if the attacker is a player.

Thanks man it worked! It was so simple but thanks for the help :slight_smile:

Code is


function GM:PlayerShouldTakeDamage( victim, attacker )

	if attacker:IsWorld() then return true
	elseif attacker:IsNPC() then return true
	elseif attacker:IsPlayer() then
	if (attacker:GetUserGroup() == victim:GetUserGroup()) then return false end
	elseif attacker:IsPlayer() then
	if (attacker:GetUserGroup() ~= victim:GetUserGroup()) then return true end
	else return true
	end
end