hi so im trying to make a script for a sword that only kill NPCs , is there a way to say like IsAPlayer? or something like this?
Maybe you can make a folder in your workspace called “NPCs” or something. Then, when you swing your sword, you can check if the NPC is in the folder. And if it is, it would damage it. Or, name the NPC and check if the NPC is that name.
1 Like
Another way you can do is by checking if the parent of the object that was touched by the sword has a humanoid by doing something like this:
Sword.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
-----The sword has Hit a humanoid
end
end)
4 Likes
He wants it to only kill NPC’s and as all players have Humanoids along with NPC’s this would not answer the question correctly.
GetPlayerFromCharacter
will return nil
if the provided Model isn’t a Player’s Character.
You can put this into action like so:
--// Dependencies
local Players = game:GetService("Players")
--// Variables
local SwordBlade = --// The blade of the sword
--// Functions
local function onTouched(hit)
local isPlayer = Players:GetPlayerFromCharacter(hit.Parent)
if not isPlayer then --// Nil acts like false, you can reverse it with not
--// They're not a player
end
end
--// Connections
SwordBlade.Touched:Connect(onTouched)
4 Likes