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

(SOLVED) How do I fix my bow hurting me when attacking an npc?

Asked by
VVTF_RU 18
3 years ago
Edited 3 years ago

Hello, I have created a bow and arrow recently and one of my scripts is not working right, Whenever I hit any NPC/Player the arrow will damage me and not the player I hit. I have 2 scripts a local script and a server script, the error is from the server script however. Here is the script

local tool = script.Parent
local handle = tool:WaitForChild('Handle')
local event = tool:WaitForChild('RemoteEvent')
local debounce = true
local cooldownTime = 0.5
local arrow = game.ReplicatedStorage.Arrow

event.OnServerEvent:Connect(function(player,pos)
    local humanoid = tool.Parent:FindFirstChildWhichIsA('Humanoid')
    if humanoid and humanoid.Health > 0 and debounce == true then
        debounce = false
        local newArrow = arrow:Clone()
        newArrow.Parent = workspace
        newArrow.CanCollide = false
        newArrow.CFrame = CFrame.new(handle.Position,pos)
        local velocity = Instance.new('BodyVelocity')
        velocity.Parent = newArrow
        velocity.Velocity = newArrow.CFrame.LookVector.Unit*75
        velocity.MaxForce = Vector3.new('inf','inf','inf')
        game.Debris:AddItem(velocity,25)
        newArrow.Touched:Connect(function(hit)
            local human = hit.Parent:FindFirstChildWhichIsA('Humanoid')
            if human and humanoid.Health > 0 and human ~= humanoid then
                humanoid:TakeDamage(55)
                newArrow:Destroy()
            elseif not human and hit.Cancollide == true and not hit:IsDescendantOf(humanoid.Parent) and not hit:IsDescendantof(tool) then
                if velocity then
                    velocity:Destroy()
                end
                local weld = Instance.new('WeldConstraint')
                weld.Parent = newArrow
                weld.Part0 = newArrow
                weld.Part1 = hit
                newArrow.Massless = true
                newArrow.CanCollide = false
            end
        end)
        game.Debris:AddItem(newArrow,10)
        wait(cooldownTime)
        debounce = true
    end
end)

and if you need to know here is the local script

local tool = script.Parent
local handle = tool:WaitForChild('Handle')
local event = tool:WaitForChild('RemoteEvent')
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

tool.Activated:Connect(function()
    event:FireServer(mouse.Hit.Position)
end)

1 answer

Log in to vote
1
Answered by 3 years ago

You accidentally keep using the variables Human (the hit target's humanoid) and Humanoid (the player's humanoid) interchangeably. This is an easy fix. Just go through the script and make sure you have the variables correct. For example, here's a fix to lines 23 & 24:

local human = hit.Parent:FindFirstChildWhichIsA('Humanoid')
            if human and human.Health > 0 and human ~= humanoid then
                human:TakeDamage(55)

In this case, you were checking if the player's health was greater than 0 and then taking the damage from the player instead of the target. Hope this helped.

0
Dude, I was so confused at first cause I thought you were talking about line 9 and 10 then I realized you said HUMAN and were referring to that one LOL. Thank you alot, that helps me so much! VVTF_RU 18 — 3y
Ad

Answer this question