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)
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
Furthermore, is this on a Server or LocalScript?
:TakeDamage(10) functionality should be in a ServerScript,
:LoadAnimation() should be on a LocalScript instead
It is recommended to :LoadAnimation() for players’ characters in Client Side only → LocalScript
You only use :LoadAnimation() on NonPlayerCharacters in Server Side
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()
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
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
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)