(SOLVED) How do I fix my bow hurting me when attacking an npc?
Asked by
4 years ago Edited 4 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
01 | local tool = script.Parent |
02 | local handle = tool:WaitForChild( 'Handle' ) |
03 | local event = tool:WaitForChild( 'RemoteEvent' ) |
05 | local cooldownTime = 0.5 |
06 | local arrow = game.ReplicatedStorage.Arrow |
08 | event.OnServerEvent:Connect( function (player,pos) |
09 | local humanoid = tool.Parent:FindFirstChildWhichIsA( 'Humanoid' ) |
10 | if humanoid and humanoid.Health > 0 and debounce = = true then |
12 | local newArrow = arrow:Clone() |
13 | newArrow.Parent = workspace |
14 | newArrow.CanCollide = false |
15 | newArrow.CFrame = CFrame.new(handle.Position,pos) |
16 | local velocity = Instance.new( 'BodyVelocity' ) |
17 | velocity.Parent = newArrow |
18 | velocity.Velocity = newArrow.CFrame.LookVector.Unit* 75 |
19 | velocity.MaxForce = Vector 3. new( 'inf' , 'inf' , 'inf' ) |
20 | game.Debris:AddItem(velocity, 25 ) |
21 | newArrow.Touched:Connect( function (hit) |
22 | local human = hit.Parent:FindFirstChildWhichIsA( 'Humanoid' ) |
23 | if human and humanoid.Health > 0 and human ~ = humanoid then |
24 | humanoid:TakeDamage( 55 ) |
26 | elseif not human and hit.Cancollide = = true and not hit:IsDescendantOf(humanoid.Parent) and not hit:IsDescendantof(tool) then |
30 | local weld = Instance.new( 'WeldConstraint' ) |
31 | weld.Parent = newArrow |
34 | newArrow.Massless = true |
35 | newArrow.CanCollide = false |
38 | game.Debris:AddItem(newArrow, 10 ) |
and if you need to know here is the local script
1 | local tool = script.Parent |
2 | local handle = tool:WaitForChild( 'Handle' ) |
3 | local event = tool:WaitForChild( 'RemoteEvent' ) |
4 | local player = game.Players.LocalPlayer |
5 | local mouse = player:GetMouse() |
7 | tool.Activated:Connect( function () |
8 | event:FireServer(mouse.Hit.Position) |