Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

1 comment

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.