Humanoid.Died:Connect() wont work

so I’m having a problem with Humanoid.Died:Connect(), i put a certain function in there and it wont print or do anything, which it seems like it would work, im also running a remote event to give them stats

code:

function onDied()
	local killer = humanoid:GetLastDamage()
	if killer and killer.Parent:FindFirstChild("leaderstats") then
		local player = killer:GetPlayerFromCharacter()
		KillEvent:FireClient(player, coinsamount.Value)
	end
	print("FiredClient")
end

	humanoid.Died:Connect(onDied)

this is only part of the code, no output errors, no errors in the code, i have no idea what could be wrong.

1 Like

Do you have any other variables under the name of “onDied”?

What is the humanoid variable?

We need to know these things for this kind of situation.

local humanoid = script.Parent.Humanoid
local NPC = script.Parent
local coinsamount = NPC.CoinsAmount

local remoteevents = game.ReplicatedStorage.Events
local KillEvent = remoteevents.KillEvent

--find first player near npc after found kill the player
function findPlayer()
    local players = game:GetService("Players"):GetPlayers()
    for i, player in ipairs(players) do
        if (player.Character and player.Character:FindFirstChild("HumanoidRootPart")) then
            local distance = (NPC.PrimaryPart.Position - player.Character.HumanoidRootPart.Position).magnitude
            if distance <= 45 then --if the player is within 10 studs of the npc kill them.
				humanoid:MoveTo(player.Character.Head.Position)
			end
			if distance <= 5 and not NPC.Humanoid:GetState(Enum.HumanoidStateType.Dead) then
				player.Character.Humanoid.Health -= 5
			end
        end
    end 
end


while wait(1) do
	findPlayer()
end
--give player coins when killed
function onDied()
	local killer = humanoid:GetLastDamage()
	if killer and killer.Parent:FindFirstChild("leaderstats") then
		local player = killer:GetPlayerFromCharacter()
		KillEvent:FireClient(player, coinsamount.Value)
	end
	print("FiredClient")
end

while wait(.5) do
	humanoid.Died:Connect(onDied)
end


sorry, didn’t really think about that, but no i dont have any other “onDied” variables

You dont need to use a while-loop on it.

Try switching that out for a function that detects when a character respawns, such as CharacterAdded, if you want to avoid this entirely, just put the script in StarterCharacterScripts.

this is for an npc, not a player, the npc when died, needs to give the player 1 kill and coins which i have on a remote event script

remote event code:

local remoteevents = game.ReplicatedStorage.Events

remoteevents.KillEvent.OnServerEvent:Connect(function(player, coinsamount)
	player.leaderstats.Kills += 1
	player.leaderstats.Coins += coinsamount
end)

This is preventing your script from continuing. A loop will always yield the thread until it stops. Wrap it in a new thread via coroutines or the task library.

This method does not exist

This is not how you use GetPlayerFromCharacter. It needs to be called from the Players service with the argument being the player’s character.

You should NOT be connecting new event connections like this; this will just eat up the game’s memory and likely crash it once the event fires. Just connect it once.

1 Like

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