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

Why won't the NPC Battle End?

The point of the script it to activate the battle once a player touches the NPC part. The problem is that upon the NPC's death to end the battle, the battle still continues, ignoring the if statement under the function.

If it helps, here is a gif I created to help you understand the problem. https://gyazo.com/494f8e059ced73cb8c8e2f52f5d03c6b

script.Parent.Anchored = true
script.Parent.CanCollide = false

local CombatActive = false

function CombatInitiate(part)
	local human = part.Parent:FindFirstChild("Humanoid" and "Torso")
	if human ~= nil then
		if CombatActive == false then
			CombatActive = true -- Disables function if activated
			
			-- Bad Brick Properties
			script.Parent.Anchored = true
			script.Parent.CanCollide = false
			script.Parent.Position = script.Parent.Parent.Parent.BattleField.EnemyPlatform.Position
			print("Combat Simulation Successful")
		
			-- Local Player Properties
			part.Parent.Humanoid.WalkSpeed = 25
			part.Parent.Torso.CFrame = script.Parent.Parent.Parent.BattleField.PlayerPlatform.CFrame
		
			-- Battle Theme
			script.Parent.Parent.Parent.BattleTheme:Play()
			
		elseif CombatActive == true then 
		return end -- Prevents mutliple battles on the same opponent
			
	end	
end

if script.Parent.Parent.Humanoid.Health <= 0 then
	script.Parent.Parent.Parent.BattleTheme:Stop()
	script.Parent.Position = script.Parent.Parent.Parent.EnemySpawn.Position
	wait(5)
	CombatActive = false
end


script.Parent.Touched:connect(CombatInitiate)

1 answer

You're not checking when it dies

I know, I know, you're totally checking if it's dead, I get that. You're not checking if it dies. To do that, you'll need Humanoid.Died

Instead of

if script.Parent.Parent.Humanoid.Health <= 0 then
    script.Parent.Parent.Parent.BattleTheme:Stop()
    script.Parent.Position = script.Parent.Parent.Parent.EnemySpawn.Position
    wait(5)
    CombatActive = false
end

Do

script.Parent.Parent.Humanoid.Died:connect(function()
    script.Parent.Parent.Parent.BattleTheme:Stop()
    script.Parent.Position = script.Parent.Parent.Parent.EnemySpawn.Position
    wait(5)
    CombatActive = false
end)

Because that listens for when the Humanoid is killed.

2 comments

0

I'm not sure if I'm too inexperienced with humanoids and their properties to understand, but the improved script doesn't call the function upon the humanoid's death when I tested it (I assume the .Died event activates when the humanoid reaches 0 health).

1

The died event activates when the Humanoid dies.

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