Is there any way to make an immunity besides using forcefield?

I want to know how to make it, I know how to use forcefields but is there really another way besides using forcefields? If there’s no better way, I would use forcefields lol.

1 way would be to just use a table to know if a character is immune

local immune = {}

local function Damage(character, amount)
	-- if the character is immune then exit the function
	if immune[character] == true then return end
	 -- damage the character
	character.Humanoid:TakeDamage(amount)
end

-- get the character
local character = ...
-- damage the character by 10 damage
Damage(character, 10)
-- make the character immune
immune[character] = true
-- try to damage the character
Damage(character, 10)
-- make the character not immune
immune[character] = nil
-- damage the character by 10 damage
Damage(character, 10)

another way would be to save a attribute to the character Instance Attributes | Roblox Creator Documentation

local function Damage(character, amount)
	-- if the character is immune then exit the function
	if character:GetAttribute("Immune") == true then return end
	 -- damage the character
	character.Humanoid:TakeDamage(amount)
end

-- get the character
local character = ...
-- damage the character by 10 damage
Damage(character, 10)
-- make the character immune
character:SetAttribute("Immune", true)
-- try to damage the character
Damage(character, 10)
-- make the character not immune
character:SetAttribute("Immune", nil)
-- damage the character by 10 damage
Damage(character, 10)

another option is to use BoolValue | Roblox Creator Documentation

1 Like