Damage not working

So basically im trying to make a part damage a enemy when touched (not a player), and when I test them on them it doesn’t do damage. (Theres no errors either)
Script:


In the prints when I check the logs it only gets to “hit them fr” and nothing else happens.

The hitbox is collision-off and anchored by the way.

4 Likes

maybe you disabled CanTouch or CanQuery? (in hitbox properties)

2 Likes

By doing return in line 17 you prevent the code below to execute

2 Likes

No they are on and if I did then the print wouldn’t work but it does for some reason

2 Likes

So should i do if not player then instead?

1 Like

If you want to prevent the code below to execute when the player is nil then yes.

Like this:

if not player then return end

1 Like
local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if player then return end --> ? if player was found then return?

Is it supposed to be like that? If the player was found then you are basically stopping the whole code

1 Like

The point of the code is to make sure that if the hitbox hits a player then it won’t do damage, It is only going to be doing damage to npcs

1 Like

Yes that is true, but essentially doing

if player then return end

The rest of the script won’t run because you’re telling the script to stop executing that code block if a part’s that is a descendant of a player is found, if you’re not looking to damage any players why bother even checking if the part that is touched is a descendant of a player? Simply remove that line and only check if that part’s descendant is the enemy’s .

1 Like


So would this work instead?

2 Likes

I think you’re really overcomplicating this, a lot of unnecessary iterations for what!

if game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent) == nil then
    local EnemyHumanoid = Hit.Parent:FindFirstChild("Humanoid")
    if EnemyHumanoid and EnemyHumanoid.Health > 0 then
        EnemyHumanoid:TakeDamage(20)
    end
end

It’s important that you make sure the enemy still has an existing humanoid incase it dies in between of damaging / killing it or else the script will not run after for the rest.

2 Likes

If that’s the case then, the line “if player then return end” shouldn’t cause any problems. The problem here is your canHIt variable.

2 Likes

You can check if the hit is a humanoid but not a player like this:

if hit.Parent:FindFirstChild("Humanoid") and not game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
		
	--code
		
end
2 Likes

Wrong,

if player then return end

Makes the script return nil / stop running since the code after this statement is still under the same code block.

2 Likes

I don’t get how it is the problem though, since in my other weapon I use the same script and it works perfectly fine, the only difference being the script isn’t in the damage part, its working from a folder

2 Likes

@abclike123ye Did you forget to change canHit back to true? Cuz it seems like you didn’t state there anywhere in the code thus potentially stopping your whole code with “if canHit == false”.

Plus yeah if you want to check whether a humanoid was a player or not you can do Squally’s way

2 Likes

No dude, it will only return if a player is FOUND from the character. If not then it’ll run normally

2 Likes

Well besides the statement, do you have a TouchEnded event to re-enable CanHit?

2 Likes

Yeah you’re right I forgot this was a Touched event, I thought it was a loop oops.

2 Likes

Yeah it only hits once, but even so I don’t get how the script just stops at the print, even if i delete the player check

2 Likes