How to prevent damaging myself from my own attack?

I made an attack where the player spawns a part in the shape of a ball in front of him. Whenever someone touches that part he gets launched up and receives damage. The problem now is that the player that uses the attack also receives damage when touching the part. So my question is on how I can prevent that from happening.

Damage code:
script.Parent.Touched:Connect(function(hit, Player)
script.Parent:Destroy()
local char = hit.Parent
local hum = char:FindFirstChild(“Humanoid”)
if hum and char then
hum:TakeDamage(20)
local BV = Instance.new(“BodyVelocity”,hit.Parent.HumanoidRootPart)
BV.Velocity = hit.Parent.HumanoidRootPart.CFrame.upVector * 20
BV.MaxForce = Vector3.new(5000000,999999999,5000000)
BV.P = 100
game.Debris:AddItem(BV,.5)
script.Disabled = true
wait(.1)
script:Destroy()
end
end)

You could check if the name of the player is the name of the player who spawned the ball, if so, return end.

Also, is this done on the server or on the client?

The damage is done on the server.

When you create the part, make a “Creator” StringValue and store the player’s name. Then when damaging, check if the player’s name is not the creator before applying damage.

1 Like
local player = GetPlayerFromCharacter:(char)

If player and player.Name ~= Player.Name then
   -- Rest of the code
1 Like

I recommend that you keep server scripts in ServerScriptService since exploiters won’t be able to see what’s inside the script

It works now, thank you all for you’re help!