Enemies are hard to hit

hey, so i made a sword , and a dummy (to test damage), the problem is, that it takes sooo much time for the dummy to get hit, so i want to improve it and make the dummy always get hit when it suppose to…

here’s what i mean:

https://gyazo.com/138567e81894bd4f6d513f3723484de2

here is the scripts and the locatios of all:

the dummy:

DamageEncounter Script:

local humanoid = script.Parent
local cHealth = humanoid.Health

humanoid.HealthChanged:Connect(function(health)
	local Gui = script.DamageCounter:Clone()
	local change = math.floor(cHealth - health)
	if change == 0 then return end
	Gui.Parent = humanoid.Parent.Head
	Gui.TextLabel.Text = change
	humanoid.Health = humanoid.MaxHealth
	wait(1)
	for i = 0,1, .1 do
		Gui.TextLabel.TextTransparency = i
		Gui.TextLabel.TextStrokeTransparency = i
		wait()
	end
end)

the sword:

sword script:

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://3493431823"
local trail = script.Parent.Handle.Trail
local animTrack
local tool = script.Parent
local char
local debounce = false
tool.Activated:Connect(function()
if not debounce then
debounce = true
local char = tool.Parent
animTrack = char.Humanoid:LoadAnimation(animation)
trail.Enabled = true
animTrack:Play()
local dmg = script.Damage:Clone()
dmg.Parent = tool.blade
dmg.Disabled = false
wait(animTrack.Length)
if dmg then
dmg:Destroy()
end
trail.Enabled = false
wait(1)
debounce = false
end
end)
tool.Unequipped:Connect(function()
if animTrack then
animTrack:Stop()
end
end)

damage inside the sword script:

local weapon = script.Parent
local dmg = 5

weapon.Touched:connect(function(part)
	if part.Parent:findFirstChild("Humanoid") then
		local humanoid = part.Parent:findFirstChild("Humanoid")
		humanoid:TakeDamage(dmg)
		script:Destroy()
	end
end)

and that’s all, thanks in advance :slight_smile:

1 Like

I believe it’s because .Touched won’t fire if it’s already touching the object. If this is the problem you can fix it by going through all touching parts at the start of the damage script.
https://developer.roblox.com/en-us/api-reference/function/BasePart/GetTouchingParts

Also you don’t need to damage scripts separately, you can connect it in the original script and your code will be more together.

1 Like

.Touched can be a finicky event to use. What I would do instead if create an invisible hitbox part for your enemy, and then instead of using any collision events I would use a raycast from the player’s torso.

This wouldn’t perfectly simulate a swinging weapon of course, but it would:

  • Give the script (and therefore the player) instant feedback as to whether they have hit the dummy or not
  • Allow you to avoid collision events, which can be frustrating sometimes (although Roblox is pretty good with this)
  • Separate the visual swinging of the weapon from the mechanics of hitting the dummy

so basically the problem in my damage script right? , and how i use that raycast thing?

Yeah, the problem which you where describing is in your damage script.

If you want to get a solid handle on raycasting, open up a new, empty place in Roblox Studio and follow this tutorial on making a laser gun., which will give you the understanding needed to use :FindPartOnRay().

oh god, after 3 hours i finally managed to make this ray thing and it’s really worked , it made me a headache but at least i finally solved it, thanks

1 Like

Congratulations on dedicating yourself and making it work - I’m glad to have helped!

1 Like