Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[Solved] Why is my damage script not damaging the enemy?

So i made a boss fight where you have to position yourself so that when the enemy summons a spike, it impales itself dealing damage but if you get hit by it, you take damage.

The issue here is that damaging players working but damaging itself is not

I don't understand why it's not.

there is a value called HP inside the enemy model the script is inside the enemy model spike is defined as the summoned spike earlier in the code which works fine

Code:

local hit = false
local behit = false
spike.Touched:Connect(function(part)
	if game.Players:FindFirstChild(part.Parent.Name) then
		if hit == false then
			hit = true
			part.Parent.Humanoid.Health = part.Parent.Humanoid.Health - (15+(level*5))
		end
	end
	if part.Parent == script.Parent then
		if behit == false then
			behit = true
			script.Parent.HP.Value = script.Parent.HP.Value - math.random(50,750)
		end
	end
end)

Issue has been resolved, check comments to see how.

17 comments

0

Any errors in output?

0

nope

0

It can't damage the boss completely?

0

nope, it takes 0 damage but players can take damage

0

Where is this script located? A model, the boss, or the spike?

0

it's inside the boss as stated in the question

0

Is it possible to show the whole script? It doesn't seem to have any problem here.

0

the rest of the script works fine, it's this section that has the problem

0

Try to print something before if part.Parent == script.Parent then and after it so we can see where the script stopped.

0

it seems like it doesn't even register it touching the boss

0

the touched event does not fire when it touches the boss, confirmed through print statements

0

Is the spike a model?

0

it is a meshpart

0

oh so it's just roblox's dumb touched function

0

Or you should say it is Roblox MeshPart fault?

0

ok so i unachored the spike and it actually works fine now. it even gave it a cool falling down animation without it tipping over, now how do i close this question?

1 answer

you clarified some of your variables wrong, look at this and read the messages inside the script which will show you what you did wrong

local hit = true --was false before
local behit = false
level = 0 --you might wanna remove this idk
spike = nil--put location of spike here
spike.Touched:Connect(function(part)
	if game.Players:FindFirstChild(part.Parent.Name) then
		if hit == false then --again, you just called out hit false in the script you showed us, make it true, when it damages make it go false so there's no damage flooding.
			hit = false --was true before, make hit flaseright aftet it hits, so this functioon cant be flooded like i sayed.
			part.Parent:FindFirstChild("Humanoid").Health = part.Parent:FindFirstChild("Humanoid").Health - 15+level*5 
		end
	end
	if part.Parent == script.Parent then
		if behit == false then
			behit = true
			script.Parent.HP.Value = script.Parent.HP.Value - math.random(50,750)
		end
	end
end)

you might want to edit a lot of stuff I just send above since I don't know half of what's going on here, do that correctly and it will work as you want it to.

2 comments

0

that does not fix the issue that i am having, the player damaging works fine but damaging the NPC (2nd half) was not working

0

Well, level and spike seems to be unlisted here, not something did wrong. Also, if hit == false then hit = true is a debounce but you made it false two times so it can't make the effect of debounce. Please ask the asker for more information before answering.

This discussion is preserved as it was. New answers, comments, and votes are closed.