How do I prevent self damage?

Yes I did search this up already, but found no results. I’m trying to make a projectile. But, it damages yourself if you touch it. I know I can add a bool value with the player’s username, but I don’t want it to just destroy the projectile if it hits me.

Please help, and have a great day.

1 Like

just add a line like

if not plr then
---whatever the rest of the script was
end

as a check before damaging or destroying the projectile.

How would that stop it from damaging myself or the sender?

How is your code structured right now? Are you using the .Touched event? I’d like more details if possible. And you can easily add a bool at the begin to check if the hit model is your character, if it return false then just keep going what the projectile is supposed to do.

I’m using a .Touched event. Also I’d like to see more about the hit model is your character.
(I might not respond as much since I’ll be afk).

local Character = Player.Character;
Bullet.Touched:Connect(function(Hit)
   if Hit.Parent ~= Character then
      -- hit isn't character, keep going
   end;
end);

I don’t know how your code is structured right now, but you should be able of “translating” my version into yours.

1 Like
local Bullet = script.Parent
local Damage = 15

local function TagHumanoid(Humanoid)
	local CreatorTag = Bullet:FindFirstChild("creator")
	if CreatorTag ~= nil then
		local NewCreatorTag = CreatorTag:Clone()
		NewCreatorTag.Parent = Humanoid
	end
end

function UntagHumanoid(Humanoid)
	if Humanoid ~= nil then
		local CreatorTag = Humanoid:FindFirstChild("creator")
		if CreatorTag ~= nil then
			CreatorTag:Destroy()
		end
	end
end

local function OnTouched(Hit)
	if Hit.Parent:FindFirstChild("Bullet") then return end
	local Player = Bullet.creator.Value
	local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
	if Humanoid and not Humanoid:IsDescendantOf(Player.Character) then
		Humanoid:TakeDamage(Damage)
		TagHumanoid(Humanoid)
		wait(0.25)
		UntagHumanoid(Humanoid)
	end
	Connection:Disconnect()
	Bullet:Destroy()
end

Connection = Bullet.Touched:Connect(OnTouched)
wait(3)
Bullet:Destroy()

Somehting I made this for 2 hours trying to figured out

1 Like