Why my combat system is hitting the current player?

I’m making a game that includes some combat system, but it’s hitting the current player, and i idk why, can anyone help me?

btw, i’m making a tool one not a uis one.

Script:

local arma = script.Parent
local jogador = arma.Parent
local personagem = jogador.Parent.Character
local humanoid = personagem:WaitForChild("Humanoid")
local animacoes = arma.Animations
local DB = false
local CanHit = true

local combo = 1

function CriarHitbox()
	local hrp =  personagem.HumanoidRootPart
	local Hitbox = Instance.new("Part")
	Hitbox.Parent = personagem
	Hitbox.Size = Vector3.new(5, 5, 5)
	Hitbox.Anchored = true
	Hitbox.CFrame = hrp.CFrame * CFrame.new(Vector3.new(0, 0, -5))
	Hitbox.CanCollide = false
	Hitbox.Transparency = 0.5
	Hitbox.Touched:Connect(function(hit)
		if hit ~= jogador then
			local inimigohumanoid = hit.Parent:FindFirstChild("Humanoid")
			if inimigohumanoid and CanHit == true then
				inimigohumanoid:TakeDamage(10)
				CanHit = false
				task.wait(0.3)
				CanHit = true
			end
		else
			print("Hit achado foi do jogador, não do inimigo.")
		end
	end)
	task.wait(0.15)
	Hitbox:Destroy()
end

function DarDano()
	animacaojogar.KeyframeReached:Connect(function(key)
		if key == "Hit" then
			CriarHitbox()
		end
	end)
end

arma.Activated:Connect(function()
	if DB == false then
		DB = true
		animacaojogar = humanoid:LoadAnimation(animacoes[combo])
		DarDano()
		combo += 1
		animacaojogar:Play()
		if combo >= 3 then
			combo = 1
		end
		task.wait(0.4)
		DB = false
	end
end)

Rather doing that messy bits, do this

local victimCharacter: Model = hit:FindFirstAncestorOfClass("Model")
local victimHumanoid: Humanoid = victimCharacter and victimCharacter:FindFirstChildOfClass("Humanoid")
if victimCharacter ~= personagem then -- personagem refers to your character
	victimHumanoid:TakeDamage(10)
	CanHit = false
	task.wait(0.3)
	CanHit = true
end
2 Likes

Furthermore, is this on a Server or LocalScript?
:TakeDamage(10) functionality should be in a ServerScript,
:LoadAnimation() should be on a LocalScript instead

2 Likes

why the loadanimation should be on a local script? also, yeah, the script is on the server, inside the tool

also, the hitbox is not working, it don’t hit the dummys

Roblox Official Wiki Link - Animator | Documentation - Roblox Creator Hub

It is recommended to :LoadAnimation() for players’ characters in Client Side only → LocalScript
You only use :LoadAnimation() on NonPlayerCharacters in Server Side

3 Likes

idk, the animation still works, but thanks anyways

edit: … just a question, what is that:
Screenshot_2
‘-’

also, the hitbox don’t work idk why, i didn’t get any errors

edit: oh nvm look: Players.Heitorsonic123508.Backpack.Combate.Script:22: attempt to index nil with ‘FindFirstChild’

after i changed this line:

local victimHumanoid = victimCharacter:FindFirstChild(“Humanoid”)

Usually I see a sword script to connect Touched event when player is swinging their sword
Rather than creating Hitbox as a new Part, can you do this?

local arma = script.Parent
local jogador = arma.Parent
local blade = arma.Blade --> Name the blade part as Blade
...

function CriarHitbox()
	local touchConnection = blade.Touched:Connect(function(hit)
		local victimCharacter: Model = hit:FindFirstAncestorOfClass("Model")
		local victimHumanoid: Humanoid = victimCharacter and victimCharacter:FindFirstChildOfClass("Humanoid")
		if victimCharacter ~= personagem then -- personagem refers to your character
			victimHumanoid:TakeDamage(10)
			CanHit = false
			task.wait(0.3)
			CanHit = true
		end
	end)
	task.wait(0.15)
	touchConnection :Disconnect()
2 Likes

bro, i don’t want a sword, i want to make combat (fists) bruh

but i appreciate what did you wanted to do, but okay.

Rename it as Fist?

If you want the hitbox to be in front of the player’s humanoid root part with Size (5,5,5)

You may be better off using workspace:GetPartsInPart() or workspace:GetPartsInBound() instead of just Touched, in term of efficiency and performance.

3 Likes

um, what is this gonna help me, first of all, i don’t know absolutely nothing about this thing, second, i just want to make a simple combat fist but i want to when collide, it just don’t do damage in the current player, just on the enemy. because i have like, animations and all the thingys

@DarkMatterMU is right. GetPartsInPart is a lot better in every single aspect in my opinion too, for your current issue however, you just need to check if hit.Parent is equal to ur character, if it is then return end

2 Likes

now, i’m not just, like, a beggar but, can you just give me a simple example code on how can i make that?

local Workspace = game:GetService("Workspace")

local part = Instance.new("Part")
part.Anchored = true
part.Position = Vector3.new(0, 15, 0)
part.Orientation = Vector3.new(0, 90, 0)
part.BrickColor = BrickColor.new("Bright green")
part.Parent = Workspace

local otherPart = Instance.new("Part")
otherPart.Name = "OtherPart"
otherPart.Anchored = true
otherPart.Position = Vector3.new(0, 15, 0)
otherPart.BrickColor = BrickColor.new("Bright red")
otherPart.Parent = Workspace

local filter = OverlapParams.new()
filter.FilterType = Enum.RaycastFilterType.Blacklist 
filter.FilterDescendantsInstances = { otherPart }

local foundParts = Workspace:GetPartsInPart(part, filter)
print(foundParts)
1 Like

um… UHHHHHHHH, is this simple? ok, anyways, i think i’m actually a beginner

also, what is that? filter.FilterDescendantsInstances = { otherPart }

It’s basically a filter, everything inside that table will NOT be returned by GetPartsInPart, meaning when you print it, you will see everything else inside the part, except otherPart

1 Like

oh, i see, but HOW IN THE WORLD am i’m gonna use that in my combat system???

You will simply have to learn how to use it in different scenarios, but as i said you aren’t required to use it, your original idea should still work (w maybe a couple issues)

2 Likes

bro, i just want to tell a thing, THERE’S NO TUTORIALS ON YOUTUBE, like how