enemyHumanoid.Died function only works a single time?

I have a script that’s suppose to print “killed da enemy” every time an enemy is killed and it works fine the first time they die, but after that it stops working. I’ve tried redefining the enemyHumanoid 0.5 seconds after they’re killed but it doesn’t work (the humanoid always stays where it is in the studio’s workspace).

What happens:
https://gyazo.com/017bdd05af94438ec77b96ea1ba2e3a9

Script:

local platform = script.Parent
local ringTeleportPosition = script.Parent.Parent.PlayerSpawn.Position
local respawnPosition = Vector3.new(0, 0, 0)
local playerInRing = false
local Round1Event = game.ReplicatedStorage.Round1
local Round2Event = game.ReplicatedStorage.Round2
local Round3Event = game.ReplicatedStorage.Round3
local MatchFinishedEvent = game.ReplicatedStorage.MatchFinished
local enemiesFolder = script.Parent.Parent.Enemies
local enemyHumanoid = enemiesFolder["Alvin Kleid"]:FindFirstChild("Humanoid")

local hasRound1Started = false
local hasRound2Started = false
local hasRound3Started = false

local character = nil
local humanoid = nil
local player = nil

local function manipulatePlatformAndBeams(isVisible)
	platform.BrickColor = isVisible and BrickColor.new("Bright red") or BrickColor.new("Mid gray")

	for _, child in pairs(platform:GetChildren()) do
		if child:IsA("Beam") then
			child.Enabled = not isVisible
		end
	end
end

local function Rounds()
	if hasRound1Started == true and hasRound2Started == true and hasRound3Started == true then
		MatchFinishedEvent:FireClient(player)
		playerInRing = false
		manipulatePlatformAndBeams(false)

	elseif hasRound1Started == true and hasRound2Started == true and hasRound3Started == false then
		hasRound3Started = true
		Round3Event:FireClient(player)

	elseif hasRound1Started == true and hasRound2Started == false and hasRound3Started == false then
		hasRound2Started = true
		Round2Event:FireClient(player)

	elseif hasRound1Started == false and hasRound2Started == false and hasRound3Started == false then
		hasRound1Started = true
		Round1Event:FireClient(player)
	end
end

local function onPlatformTouched(hit)
	character = hit.Parent
	humanoid = character:FindFirstChild("Humanoid")
	player = game.Players:GetPlayerFromCharacter(character)

	if humanoid and player and not playerInRing then
		playerInRing = true
		Rounds()
		local character = player.Character

		if character then
			character:SetPrimaryPartCFrame(CFrame.new(ringTeleportPosition))

			character:WaitForChild("Humanoid").Died:Connect(function()
				playerInRing = false
				hasRound1Started = false
				hasRound2Started = false
				hasRound3Started = false
				player:LoadCharacter()
			end)

			manipulatePlatformAndBeams(true)
		end
	end
end

enemyHumanoid.Died:Connect(function()
	character:SetPrimaryPartCFrame(CFrame.new(ringTeleportPosition))
	Rounds()
	print("killed da enemy")
end)

platform.Touched:Connect(onPlatformTouched)

So this is the character: :weary: , you define it on platform touched

local Character = 😩

and then your character dies so now the character that you defined is: :skull:

And a new character (:star_struck:) spawns but the variable not re-defined so the character variable still thinks that the character is: :skull:

So you have to re-define the variable when the new character spawns

Character = 🤩
2 Likes

Like I said above, I already tried redefining the humanoid on death but I get the same result.

enemyHumanoid.Died:Connect(function()
	character:SetPrimaryPartCFrame(CFrame.new(ringTeleportPosition))
	Rounds()
	print("killed da enemy")
	enemyHumanoid = enemiesFolder["Alvin Kleid"]:FindFirstChild("Humanoid")
end)

try replacing this

enemyHumanoid = enemiesFolder["Alvin Kleid"]:FindFirstChild("Humanoid")

with this:

enemiesFolder["Alvin Kleid"]:Destroy()
task.wait(.5)
enemyHumanoid = enemiesFolder:WaitForChild("Alvin Kleid"):WaitForChild("Humanoid")

This didn’t work.

enemyHumanoid.Died:Connect(function()
	character:SetPrimaryPartCFrame(CFrame.new(ringTeleportPosition))
	Rounds()
	print("killed da enemy")
	enemiesFolder["Alvin Kleid"]:Destroy()
	local newEnemy = game.ServerStorage["Alvin Kleid"]:Clone()
	local respawnLocation = workspace.NormalFightingRingKlein.Spawner
	newEnemy.Parent = enemiesFolder
	newEnemy.HumanoidRootPart.CFrame = respawnLocation.CFrame + Vector3.new(0, 5, 0)
	task.wait(0.5)
	enemyHumanoid = newEnemy
end)

wait my bad i forgot that you also have to redefine the .Died function when you’re redefining the humanoid

you can make a function like this:

local function RedefineHumanoid(HumanoidPath)
	enemyHumanoid = HumanoidPath

	enemyHumanoid.Died:Once(function() --Using once instead of connect so it doesn't lag. If you don't know what ":Once" does, you can google it.
		character:SetPrimaryPartCFrame(CFrame.new(ringTeleportPosition))
		Rounds()
		print("killed da enemy")
		enemiesFolder["Alvin Kleid"]:Destroy()
		local newEnemy = game.ServerStorage["Alvin Kleid"]:Clone()
		local respawnLocation = workspace.NormalFightingRingKlein.Spawner
		newEnemy.Parent = enemiesFolder
		newEnemy.HumanoidRootPart.CFrame = respawnLocation.CFrame + Vector3.new(0, 5, 0)
		task.wait(0.5)

		RedefineHumanoid(newEnemy:WaitForChild("Humanoid")) --Calls this function over and over everytime the humanoid dies
	end)
end

and you’ll also have to call the RedefineHumanoid(enemyHumanoid) function somewhere at the bottom of your script

1 Like

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