Sword that damages npc's in a certain model

I’m using the roblox default sword you can find in the toolbox and I was wondering how you could make it so that it only damages npc’s in a certain model, all of the other things I could find were either not working or weren’t compatible.

If you’ve seen another post like this you can link it in the comments to help also.

More Specific: Im trying to have a sword that only damages humanoids that are not a real player, I was wondering how I might do this using the roblox provided sword script

1 Like

Hello thetver, I am confused on what you mean by a sword that damages npc’s in a certain model, could you be more specific?

and also make sure to follow these steps when posting in #help-and-feedback:scripting-support.

  1. What do you want to achieve? Keep it simple and clear!

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

(in your post I didn’t see anything saying if you looked for solutions so far, if you did state that please let me know.)

1 Like

Im trying to have a sword that only damages humanoids that are not a real player, I was wondering how I might do this using the roblox provided sword script

1 Like

ah, now I understand.

it is definitely possible i’m just unaware how to make a script that does that, sorry.
I hope you have a good day. :slight_smile:

1 Like

Let’s assume you are using a .touched event to register a hit. Here’s what you could write in a server script inside the sword model:

local hitbox = script.Parent.Hitbox -- can be whatever part you want
local enemyModel = workspace.EnemyModel -- here's the model you place the enemies in
local damage = 10

hitbox.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChildOfClass('Humanoid') -- check for humanoid
    if hum and hit.Parent:IsDescendantOf(enemyModel) then -- if the enemy is inside the model you want
        hum:TakeDamage(damage)
    end
end)

You can start off with that and modify as you please. I recommend adding debounce so the enemies don’t take too much damage at once. Hope this helps :slight_smile:

1 Like

You can use game.Players:GetPlayerFromCharacter() to check if the character model is a player (it will return nil if no player is found). If the model is not that of a player, then you apply damage.

if not game.Players:GetPlayerFromCharacter(hit.Parent) then
    -- damage the NPC 
end
1 Like