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.
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
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 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.