Player hitting themself with tool

This tool works perfectly fine when you use the main attack (click) but when you use the special attack, the player repeatedly hits themselves with it.

Script:

local RagdollState = game.ReplicatedStorage:WaitForChild("RagdollState")

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local AbilityUi = script:WaitForChild("AbilityUi")
local Character
CanAttack = true
CanDmg = false

Tool.Equipped:Connect(function()
	Character = Tool.Parent
	local Player = game.Players:GetPlayerFromCharacter(Character)
	AbilityUi.Parent = Player.PlayerGui
	AbilityUi.LocalScript.Enabled = true
end)

Tool.Unequipped:Connect(function()
	AbilityUi.Parent = script
end)


Tool.Activated:Connect(function()
	if CanAttack then
		CanAttack = false
		local Humanoid = Character:WaitForChild("Humanoid")
		Humanoid:LoadAnimation(script.Animation):Play()
		task.wait(.2)
		Handle.Woosh:Play()
		task.wait(.2)
		Handle.Trail.Enabled = true
		CanDmg = true
		task.wait(1)
		CanDmg = false
		Handle.Trail.Enabled = false
		task.wait(.5)
		CanAttack = true
	end
end)

Handle.Touched:Connect(function(hit)
	if CanDmg and hit.Parent:FindFirstChild("Humanoid") ~= nil and Character:FindFirstAncestor(hit) == nil then
		local CharacterToHurt = hit.Parent
		CanDmg = false
		RagdollState:FireClient(game.Players:GetPlayerFromCharacter(CharacterToHurt), true)
		CharacterToHurt:FindFirstChild("Humanoid"):TakeDamage(10)
		if CharacterToHurt:FindFirstChild("Humanoid").Health > 50 then
			local VectorForce = Instance.new("VectorForce", CharacterToHurt:FindFirstChild("HumanoidRootPart"))
			VectorForce.Attachment0 = CharacterToHurt:FindFirstChild("HumanoidRootPart"):FindFirstChild("RootRigAttachment")
			VectorForce.RelativeTo = Enum.ActuatorRelativeTo.World
			VectorForce.Force = Handle.CFrame.RightVector * 1500
			game:GetService("Debris"):AddItem(VectorForce, 1)
		else
			local VectorForce = Instance.new("VectorForce", CharacterToHurt:FindFirstChild("HumanoidRootPart"))
			VectorForce.Attachment0 = CharacterToHurt:FindFirstChild("HumanoidRootPart"):FindFirstChild("RootRigAttachment")
			VectorForce.RelativeTo = Enum.ActuatorRelativeTo.World
			VectorForce.Force = Handle.CFrame.RightVector * 2500
			game:GetService("Debris"):AddItem(VectorForce, 1)
		end
		Handle.Slap:Play()
		task.wait(3)
		RagdollState:FireClient(game.Players:GetPlayerFromCharacter(CharacterToHurt), false)
	end
end)

local Folder = game:GetService("ReplicatedStorage"):WaitForChild("SpecialMoveEvents")

Folder.SpinSlap.OnServerEvent:Connect(function(Player)
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid")
	local Tool = Character:FindFirstChildOfClass("Tool")
	local Handle = Tool.Handle
	Humanoid.WalkSpeed = 10
	Handle.Trail.Enabled = true
	CanAttack = false
	for i = 1, 10 do
		task.wait(.3)
		CanDmg = true
		Handle.Woosh:Play()
	end
	CanDmg = false
	Handle.Trail.Enabled = false
	Humanoid.WalkSpeed = 16
	task.wait(.5)
	CanAttack = true
end)

Can’t you just check if the hit is the tools parent/character?

local RagdollState = game.ReplicatedStorage:WaitForChild("RagdollState")

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local AbilityUi = script:WaitForChild("AbilityUi")
local Character
CanAttack = true
CanDmg = false

Tool.Equipped:Connect(function()
	Character = Tool.Parent
	local Player = game.Players:GetPlayerFromCharacter(Character)
	AbilityUi.Parent = Player.PlayerGui
	AbilityUi.LocalScript.Enabled = true
end)

Tool.Unequipped:Connect(function()
	AbilityUi.Parent = script
end)


Tool.Activated:Connect(function()
	if CanAttack then
		CanAttack = false
		local Humanoid = Character:WaitForChild("Humanoid")
		Humanoid:LoadAnimation(script.Animation):Play()
		task.wait(.2)
		Handle.Woosh:Play()
		task.wait(.2)
		Handle.Trail.Enabled = true
		CanDmg = true
		task.wait(1)
		CanDmg = false
		Handle.Trail.Enabled = false
		task.wait(.5)
		CanAttack = true
	end
end)

Handle.Touched:Connect(function(hit)
	if CanDmg and hit.Parent:FindFirstChild("Humanoid") ~= nil and Character:FindFirstAncestor(hit) == nil then
        if hit:IsDescendantOf(Character) then return end
		local CharacterToHurt = hit.Parent
		CanDmg = false
		RagdollState:FireClient(game.Players:GetPlayerFromCharacter(CharacterToHurt), true)
		CharacterToHurt:FindFirstChild("Humanoid"):TakeDamage(10)
		if CharacterToHurt:FindFirstChild("Humanoid").Health > 50 then
			local VectorForce = Instance.new("VectorForce", CharacterToHurt:FindFirstChild("HumanoidRootPart"))
			VectorForce.Attachment0 = CharacterToHurt:FindFirstChild("HumanoidRootPart"):FindFirstChild("RootRigAttachment")
			VectorForce.RelativeTo = Enum.ActuatorRelativeTo.World
			VectorForce.Force = Handle.CFrame.RightVector * 1500
			game:GetService("Debris"):AddItem(VectorForce, 1)
		else
			local VectorForce = Instance.new("VectorForce", CharacterToHurt:FindFirstChild("HumanoidRootPart"))
			VectorForce.Attachment0 = CharacterToHurt:FindFirstChild("HumanoidRootPart"):FindFirstChild("RootRigAttachment")
			VectorForce.RelativeTo = Enum.ActuatorRelativeTo.World
			VectorForce.Force = Handle.CFrame.RightVector * 2500
			game:GetService("Debris"):AddItem(VectorForce, 1)
		end
		Handle.Slap:Play()
		task.wait(3)
		RagdollState:FireClient(game.Players:GetPlayerFromCharacter(CharacterToHurt), false)
	end
end)

local Folder = game:GetService("ReplicatedStorage"):WaitForChild("SpecialMoveEvents")

Folder.SpinSlap.OnServerEvent:Connect(function(Player)
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid")
	local Tool = Character:FindFirstChildOfClass("Tool")
	local Handle = Tool.Handle
	Humanoid.WalkSpeed = 10
	Handle.Trail.Enabled = true
	CanAttack = false
	for i = 1, 10 do
		task.wait(.3)
		CanDmg = true
		Handle.Woosh:Play()
	end
	CanDmg = false
	Handle.Trail.Enabled = false
	Humanoid.WalkSpeed = 16
	task.wait(.5)
	CanAttack = true
end)
1 Like

yup thats deffo one way of doing it

1 Like

just add to the if
and hit.parent ~= Character

1 Like

That’s what “Character:FindFirstAncestor(hit) == nil” is for. Any other ideas?

:FindFirstAncestor() works upwards. Meaning that it starts at the Instance's immediate Instance.Parent and works up towards the DataModel. Additionally, this will always return nil since the parameter for this function is meant to be a string. You’re passing an instance through it. However, there’s also another issue with your conditional statement. You shouldn’t be checking if the hit part (most likely the character’s limb) is an ancestor of the character - it makes no sense. Instead, you should use :IsDescendantOf(). This function will return true if the instance (in your case the hit part) is a descendant of the given ancestor and vice versa. You can replace that piece of your code with this:

-- Conditional statement on its own
if not hit:IsDescendantOf(Character) then -- This checks if the hit instance is NOT a descendant of the character
     -- Your code
end

-- Conditional statement implemented into your code
if CanDmg and hit.Parent:FindFirstChild("Humanoid") ~= nil and not hit:IsDescendantOf(Character) then
-- Rest of code
end

1 Like

Just add if hit:IsDescendantOf(Character) then return end check on the Touched connection.

1 Like

I just tried this. I figured out that the smack only hurts themselves when they hit someone else with the smack tool equipped. Any ideas?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.