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
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
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
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