Player Damage Script Not Working

In my game, I have an enemy which uses an attack.

that attack starts off as an indicator, but then activates and disappears within 0.1 seconds.

the issue is that the script in the attack is not registering the player even if the player is touching the attack.

The blue box in this image below is the hitbox of the attack which the player needs to touch in order for the damage to register.
image

Here is the script:

local Damage = 1
local CD = 1

local Damaged = {

}

script.Parent.Touched:Connect(function(Hit)
	local Player = game.Players:FindFirstChild(Hit.Parent.Name)
	if Player and script.Parent.Parent.AttackPart.Transparency == 1 then
		local Character = Player.Character
		local Humanoid = Character.Humanoid
	    Character.HumanoidRootPart.Anchored = true
		Humanoid:TakeDamage(Damage)
		task.wait(CD)
		Character.HumanoidRootPart.Anchored = false
		table.insert(RecentlyDamaged, table.getn(Damaged ) + 1, Player.Name)
		task.wait(CD)
		local FindPlayerAgain = table.find(Damaged , Player.Name)
		table.remove(Damaged , FindPlayerAgain)
else
	end
end)

How do I fix the script so that the player is actually registering?

Are there any errors? Any output at all??

Hello! Put this Script into the part that’s going to damage people.

local Touching = false
Damage = 40

script.Parent.Touched:connect(function(hit)
    hum = hit.Parent:FindFirstChild('Humanoid')
    if hum ~= nil and not Touching then
        Touching = true
        hum:takeDamage(Damage)
        Touching = false
    end
end)

If you found my answer helpful, be sure to mark it as the solution! :white_check_mark:

1 Like

Are you sure the part of the script that checks transparency doesn’t refer to the wrong part?
Maybe it gets deleted before it can reach full transparency.
Another idea might be ditching the touch event and just checking what is inside the hitbox, using GetTouchingParts() when you want to deal damage.

One potential issue with your script is that you are using the Transparency property to check if the attack is active. It’s possible that the attack is not fully transparent when the player touches it, which would cause the if condition to be false and the player would not take damage.

Instead of using the Transparency property, you could create a separate boolean value or flag that indicates whether the attack is currently active or not. You could then check this value in the if condition instead of the Transparency property.

Additionally, you may want to consider using a debounce function to prevent the player from taking damage multiple times in quick succession if they are standing in the attack for an extended period of time. This can be achieved by adding a timer that prevents the player from taking damage again until a certain amount of time has passed since the last time they took damage.

You could also consider optimizing the script by using a local variable to store the player’s character and humanoid, instead of looking them up every time the attack is touched. This will avoid unnecessary lookups and can improve the performance of the script.

Finally, you may want to consider using a separate function to handle the actual damage calculation and application, so that you can easily reuse this code in other parts of your game. This can also help to make the code more organized and easier to understand.

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