Issues - How Prevent Self Damage

Hello! I need a hand, the ball is an instance of part

How can I prevent harm to myself?
I would like to be able to damage other players but not myself, how can i do it?

Video example:

Project Organization:
image

back_SnowBall script:

         -- Touch Function for
         ball.Touched:Connect(function(hit)
			ball:Destroy()
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
								
			if player then	
				local char = player.Character
				local humanoid = char:FindFirstChild("Humanoid")
						
				if hit.Parent ~= nil then
					if humanoid ~= nil and not humanoid:IsDescendantOf(script.Parent) then
						humanoid.Health = humanoid.Health - damage
					end
				end
			end
		end)

You need to check if hit.Parent is not equal to tool.Parent.

1 Like

Something like this?
image

				if hit.Parent ~= script.Parent.Snowball.Parent then
					humanoid.Health = humanoid.Health - damage
				end
1 Like

the player keeps doing damage to himself, what would be wrong?

It would be better to store the snowball in a variable.

local tool = script.Parent

-- Inside the event
if hit.Parent == tool.Parent then return end

And I just notice, why would you go to Snowball and just go back to parent?

do:

if hit.Parent ~= script.Parent.Snowball.Parent.Parent then
					humanoid.Health = humanoid.Health - damage
				end
1 Like

You needed to add .Parent 2 times

1 Like

Thanks brother! you saved me, now it works perfectly :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.