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

Is There a Way to Be Able to Make the R15 Knife *Works* to Only do Damage to NPCs?

Asked by 3 years ago

I've tried and tried to be able to insert a damage check that makes sure that the knife doesn't do any damage to players but I just can't get it to work. Please help.

Thank you!

(R15 Knife Works script)

local parent = script.Parent
local Handle = parent:WaitForChild("Handle")
local Creator = parent:WaitForChild("Creator")
CanDmg = false
Can = true
parent.Equipped:Connect(function()
    Handle.Equip:Play()
    if Creator.Value == nil then
        Creator.Value = parent.Parent end
end)
parent.Unequipped:Connect(function()
    Handle.UnEquip:Play()
end)
repeat wait() until Creator.Value ~= nil
parent.Activated:Connect(function()
    if Handle.Transparency == 1 then return end
    function slash()
        print("hip")
        local creator = Creator.Value
        local Animations = parent:WaitForChild("Animations")
        local AnimChild = Animations:GetChildren()
        local AnimRad = math.random(1,#AnimChild)
        local AnimSel = AnimChild[AnimRad]
        local AP = creator:WaitForChild("Humanoid"):LoadAnimation(AnimSel)
        if Can == true then
            Can = false
            AP:Play()
            wait(0.15)
            CanDmg = true
            Handle.Swing:Play()
            Handle.SlashTrail.Enabled = true
            wait(0.25)
            CanDmg = false
            Handle.SlashTrail.Enabled = false
            wait(0.06)
            Can = true
        end
    end
    slash()
end)
function on(t)
    local h = t.Parent:FindFirstChildOfClass("Humanoid")
    if h ~= nil and CanDmg == true then
        CanDmg = false
        local cre = Creator.Value
        h:TakeDamage(parent.Dmg.Value)
        Handle.Swing:Stop()
        Handle.Hit:Play()
        if h.Health>0 then
            if not h:FindFirstChild("creator") then
                local ov = Instance.new("ObjectValue",h)
                ov.Name = "creator"
                ov.Value = game.Players:WaitForChild(cre.Name)
            else
                local ovs = h:GetChildren()
                for i = 1,#ovs do
                    if (ovs[i].Name == "creator") then
                        ovs[i].Value = game.Players:WaitForChild(cre.Name) end
                end
            end
        end
    end
end
Handle.Touched:Connect(on)

2 answers

Log in to vote
0
Answered by 3 years ago
h:TakeDamage(not game.Players:GetPlayerFromCharacter(h.Parent)and parent.Dmg.Value or 0)
0
tysm! MrSarp_shoot 37 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Rename the NPC humanoid name to something that cant be confused with the players' humanoid.

Example:

local Knife = game.Workspace.Knife

Knife.Touched:Connect(function(p)

    if p.Parent:FindFirstChild("NPCHumanoid") then

        p.Parent.NPCHumanoid:TakeDamage(25)

    end

end)

That way if the knife does touch a player it wont damage the player because the players' humanoid wont be detected.

Answer this question