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

How to make my sword not do damage to players, but to npc's?

Asked by 2 years ago

I want my sword to not damage other players. I asked this question already, but I forgot to add the part about wanting it to do damage to NPC's. Sorry! ????

Here are the scripts it currently has: In ServerScriptService (normal script)



local cooldowns = {} game.Players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) local m6d = Instance.new("Motor6D", character.RightHand) m6d.Part0 = character.RightHand m6d.Name = "ToolM6D" end) end) game.ReplicatedStorage.SwordRE.OnServerEvent:Connect(function(player, instruction, bodyAttach) if instruction == "connectm6d" then player.Character.RightHand.ToolM6D.Part1 = bodyAttach elseif instruction == "disconnectm6d" then player.Character.RightHand.ToolM6D.Part1 = nil elseif instruction == "attack" then if cooldowns[player] then return end cooldowns[player] = true game.ReplicatedStorage.SwordRE:FireAllClients(bodyAttach) local raycastParams = RaycastParams.new() raycastParams.FilterDescendantsInstances = {player.Character} local raycastResults = workspace:Raycast(player.Character.HumanoidRootPart.Position, player.Character.HumanoidRootPart.CFrame.LookVector * 3, raycastParams) if raycastResults and raycastResults.Instance.Parent:FindFirstChild("Humanoid") then raycastResults.Instance.Parent.Humanoid:TakeDamage(20) end wait(0.5) cooldowns[player] = false end end)

In StarterPack (local script)

local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

local idleAnim = character:WaitForChild("Humanoid"):LoadAnimation(script.IdleAnimation)

local animCombo = {
    character:WaitForChild("Humanoid"):LoadAnimation(script.Swing1Animation),
    character:WaitForChild("Humanoid"):LoadAnimation(script.Swing2Animation),
    character:WaitForChild("Humanoid"):LoadAnimation(script.Swing3Animation),
}

local currentCombo = 1


script.Parent.Equipped:Connect(function()

    game.ReplicatedStorage.SwordRE:FireServer("connectm6d", script.Parent.BodyAttach)

    character.RightHand.ToolM6D:GetPropertyChangedSignal("Part1"):Wait()
    idleAnim:Play()
end)


script.Parent.Unequipped:Connect(function()

    game.ReplicatedStorage.SwordRE:FireServer("disconnectm6d")

    idleAnim:Stop()
end)


local cooldown = false

script.Parent.Activated:Connect(function()

    if cooldown then return end

    cooldown = true

    game.ReplicatedStorage.SwordRE:FireServer("attack", script.Parent.BodyAttach)

    animCombo[currentCombo]:Play()

    character.HumanoidRootPart.Velocity = character.HumanoidRootPart.CFrame.LookVector * 100 * Vector3.new(1, 0, 1)

    currentCombo += 1
    if currentCombo > #animCombo then currentCombo = 1 end

    local previousCombo = currentCombo

    spawn(function()

        wait(3)

        if currentCombo == previousCombo then
            currentCombo = 1
        end
    end)

    wait(0.5)

    cooldown = false
end)


game.ReplicatedStorage.SwordRE.OnClientEvent:Connect(function(bodyAttach)

    bodyAttach.Slash:Play()

    bodyAttach.Trail.Enabled = true
    wait(0.3)
    bodyAttach.Trail.Enabled = false
end)

Thanks!

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

A simple fix is to change the name of the humanoid of ur npc that needs attacking then tell the script to detect that humanoid and if it does then dmg Example: if raycastResults and raycastResults.Instance.Parent:FindFirstChild("SorryHuman") then In this case i am assuming u will rename the npc humanoid to "SorryHuman"

Ad
Log in to vote
0
Answered by
enes223 327 Moderation Voter
2 years ago
Edited 2 years ago

You can check if the character is a player's character, example:

-- Raycast stuff bla bla
if game:GetService("Players"):GetPlayerFromCharacter(Result.Parent) then return end
-- Do damage and stuff

Answer this question