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

For some reason, my weapon keeps deflecting the attack, meaning It will attack you only (?)

Hello! I have a Issue

So this script you see below Is for a fishing rod, basically, I've modify the script so that It won't attack people from your team, for some reason however, If you attack a player from the opposite team, the attack will deflect and damage you Instead, for example, Player1 Is In red team while Player2 Is In blue team, Player1 got a knife, If Player1 tries to stab Player2, Player2 will be fine as the weapon deflects the attack and Player1 gets damage Instead

script:

local Tool = script.Parent
local Animation = Tool.Animation
local CanDamage = false

Tool.RemoteEvent.OnServerEvent:Connect(function()
	Tool.Sounds.Woosh:Play()
	local Swing = Tool.Parent.Humanoid:WaitForChild("Animator"):LoadAnimation(Tool.Animation)
	Swing:Play()
	CanDamage = true
	Tool.HitBox.Touched:Connect(function(hit)
		local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		if Player then
			local player1 = game.Players:GetPlayerFromCharacter(hit.Parent)
			local player2 = game.Players:GetPlayerFromCharacter(Tool.Parent)

			if (player1 ~= nil) and (player2 ~= nil) then
				local humanoid = (player2.Character or player2.CharacterAdded:Wait()):FindFirstChildWhichIsA("Humanoid")

				if (player1.TeamColor ~= player2.TeamColor) then
					if (humanoid ~= nil) and CanDamage == true then
						Tool.Sounds.Impact:Play()
						local GodOw = Tool.Sounds["God Damn"]:Clone()
						game:GetService("Debris"):AddItem(GodOw,2)
						GodOw.Parent = humanoid.Parent.Torso
						GodOw:Play()
						humanoid:TakeDamage(35)
						local Weld = Instance.new("Weld",humanoid.Parent.Torso)
						game:GetService("Debris"):AddItem(Weld,0.3)
						Weld.Part0 = humanoid.Parent.Torso
						Weld.Part1 = Tool.HitBox
						CanDamage = false
						humanoid.PlatformStand = true
						wait(1)
						humanoid.PlatformStand = false
					end
				end
			end
		elseif not Player then
			local humanoid = hit.Parent:WaitForChild("Humanoid")
			if humanoid ~= Tool.Parent.Humanoid and CanDamage == true then
				Tool.Sounds.Impact:Play()
				local GodOw = Tool.Sounds["God Damn"]:Clone()
				game:GetService("Debris"):AddItem(GodOw,2)
				GodOw.Parent = humanoid.Parent.Torso
				GodOw:Play()
				humanoid:TakeDamage(35)
				local Weld = Instance.new("Weld",humanoid.Parent.Torso)
				game:GetService("Debris"):AddItem(Weld,0.3)
				Weld.Part0 = humanoid.Parent.Torso
				Weld.Part1 = Tool.HitBox
				CanDamage = false
				humanoid.PlatformStand = true
				wait(1)
				humanoid.PlatformStand = false
			end
		end
	end)
	wait(1.3)
	CanDamage = false
end)

--[[ Original Damage Script
Tool.HitBox.Touched:Connect(function(hit)
		local humanoid = hit.Parent:WaitForChild("Humanoid")
		if humanoid ~= Tool.Parent.Humanoid and CanDamage == true then
			Tool.Sounds.Impact:Play()
			local GodOw = Tool.Sounds["God Damn"]:Clone()
			game:GetService("Debris"):AddItem(GodOw,2)
			GodOw.Parent = humanoid.Parent.Torso
			GodOw:Play()
			humanoid:TakeDamage(35)
			local Weld = Instance.new("Weld",humanoid.Parent.Torso)
			game:GetService("Debris"):AddItem(Weld,0.3)
			Weld.Part0 = humanoid.Parent.Torso
			Weld.Part1 = Tool.HitBox
			CanDamage = false
			humanoid.PlatformStand = true
			wait(1)
			humanoid.PlatformStand = false
		end
	end)
]]

1 answer

✓ Accepted answer

I just started looking at your script and I already noticed a problem. In line 17, you chose your (player 2) humanoid instead of player 1's. Copy this whole code, paste into your script and it should work.

local Tool = script.Parent
local Animation = Tool.Animation
local CanDamage = false

Tool.RemoteEvent.OnServerEvent:Connect(function()
Tool.Sounds.Woosh:Play()
local Swing = Tool.Parent.Humanoid:WaitForChild("Animator"):LoadAnimation(Tool.Animation)
Swing:Play()
CanDamage = true
Tool.HitBox.Touched:Connect(function(hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if Player then
		local player1 = game.Players:GetPlayerFromCharacter(hit.Parent)
		local player2 = game.Players:GetPlayerFromCharacter(Tool.Parent)
		
		if (player1 ~= nil) and (player2 ~= nil) then
			local humanoid = (playe1.Character or player1.CharacterAdded:Wait()):FindFirstChildWhichIsA("Humanoid")
			
			if (player1.TeamColor ~= player2.TeamColor) then
				if (humanoid ~= nil) and CanDamage == true then
					Tool.Sounds.Impact:Play()
					local GodOw = Tool.Sounds:FindFirstChild("God Damn"):Clone()
					game:GetService("Debris"):AddItem(GodOw,2)
					GodOw.Parent = humanoid.Parent.Torso
					GodOw:Play()
					humanoid:TakeDamage(35)
					local Weld = Instance.new("Weld",humanoid.Parent.Torso)
					game:GetService("Debris"):AddItem(Weld,0.3)
					Weld.Part0 = humanoid.Parent.Torso
					Weld.Part1 = Tool.HitBox
					CanDamage = false
                        		humanoid.PlatformStand = true
					task.wait(1)
					humanoid.PlatformStand = false
				end
			end
		end
	elseif not Player then
		local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
		if humanoid ~= Tool.Parent.Humanoid and CanDamage == true then
				Tool.Sounds.Impact:Play()
				local GodOw = Tool.Sounds:FindFirstChild("God Damn"):Clone()
				game:GetService("Debris"):AddItem(GodOw,2)
				GodOw.Parent = humanoid.Parent.Torso
				GodOw:Play()
				humanoid:TakeDamage(35)
				local Weld = Instance.new("Weld",humanoid.Parent.Torso)
				game:GetService("Debris"):AddItem(Weld,0.3)
				Weld.Part0 = humanoid.Parent.Torso
				Weld.Part1 = Tool.HitBox
				CanDamage = false
				humanoid.PlatformStand = true
				task.wait(1)
				humanoid.PlatformStand = false
			end
		end
	end)
	task.wait(1.3)
	CanDamage = false
end)

--[[ Original Damage Script
Tool.HitBox.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid ~= Tool.Parent.Humanoid and CanDamage == true then
		Tool.Sounds.Impact:Play()
		local GodOw = Tool.Sounds:FindFirstChild("God Damn"):Clone()
		game:GetService("Debris"):AddItem(GodOw,2)
		GodOw.Parent = humanoid.Parent.Torso
		GodOw:Play()
		humanoid:TakeDamage(35)
		local Weld = Instance.new("Weld",humanoid.Parent.Torso)
		game:GetService("Debris"):AddItem(Weld,0.3)
		Weld.Part0 = humanoid.Parent.Torso
		Weld.Part1 = Tool.HitBox
		CanDamage = false
		humanoid.PlatformStand = true
		task.wait(1)
		humanoid.PlatformStand = false
	end
end)
]]

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