Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make a sword that doesn't damage other players, only NPCs?

Asked by 4 years ago

I'm making an RPG, but I'm still a beginner scripter and I don't know how to make it check if it's a player or a npc. I'd like it to only hit the npc and not other players.

Here's the damage script of the sword:

local Tool = script.Parent
local Time = 0
local C0 = script.Parent.Combo.C0
local C1 = script.Parent.Combo.C1
local Debris = game:GetService("Debris")
local players = game:GetService("Players")

C0.OnServerEvent:Connect(function(plr,Part,d)
    Enable = true
    Time = 0
        Part.Touched:Connect(function(hit)
        if Time == 0 and Enable == true then
            if hit.Parent:FindFirstChild("Humanoid") ~= nil and hit.Parent:WaitForChild("Humanoid").Health ~= 0 and not hit.Parent:FindFirstChild("AlreadyHit") then
                local Check = Instance.new("IntValue", hit.Parent)
                Check.Name = "AlreadyHit"
                game.Debris:AddItem(Check ,0.625)
                local vCharacter = Tool.Parent
                local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)
                local Humanoid = hit.Parent:WaitForChild("Humanoid")
                local sound = script.Hit:Clone()
                sound.Parent = hit.Parent
                sound:Play()
                TagHumanoid(Humanoid, vPlayer)
                Humanoid:TakeDamage(script.Parent.Damage.Value)
                wait(0.25)
                UntagHumanoid(Humanoid)
            end
       end
    end)
end)
C1.OnServerEvent:Connect(function(plr,Part,d)
    Enable = true
    Time = 0
        Part.Touched:Connect(function(hit)
        if Time == 0 and Enable == true then
            if hit.Parent:FindFirstChild("Humanoid") ~= nil and hit.Parent:WaitForChild("Humanoid").Health ~= 0 and not hit.Parent:FindFirstChild("AlreadyHit") then
                local Check = Instance.new("IntValue", hit.Parent)
                Check.Name = "AlreadyHit"
                game.Debris:AddItem(Check ,1)
                local vCharacter = Tool.Parent
                local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)
                local Humanoid = hit.Parent:WaitForChild("Humanoid")
                local sound = script.Hit:Clone()
                sound.Parent = hit.Parent
                sound:Play()
                TagHumanoid(Humanoid, vPlayer)
                Humanoid:TakeDamage(script.Parent.Damage.Value)
                wait(0.25)
                UntagHumanoid(Humanoid)
            end
       end
     end)
end)

function TagHumanoid(humanoid, player)
    local creator_tag = Instance.new("ObjectValue")
    creator_tag.Value = player
    creator_tag.Name = "creator"
    creator_tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
    if humanoid ~= nil then
        local tag = humanoid:findFirstChild("creator")
        if tag ~= nil then
            tag.Parent = nil
        end
    end
end

while wait(1) do
    Time = Time + 1
    if Time == 0 then
        Time = 1
    end
end

1 answer

Log in to vote
0
Answered by 4 years ago

Try checking if the NPC is in the player folder, if it isn't then it's an NPC (since they aren't actual players)

-- localscript in sword

-- inside the blade touch event or whatever you use to detect sword hits, "hit" being the object that got hit

if hit.Parent:FindFirstChild("Humanoid") then
    -- its some sort of player/NPC
    if game.Players:FindFirstChild(hit.Parent.Name) then
        -- its an actual player
    else
        -- not in the players folder, NPC
        -- damage code goes here.
    end
end)

the code I wrote is obviously very simplified but the idea remains, check the name of the parent of the hit (for example, the sword hits a torso, the parent would be the character) and check if it's name is in the player folder. if it is, it's a real player, if not, it's an NPC.

0
Wow, thanks a lot! SherrKar 2 — 4y
Ad

Answer this question