Script cant detect when player dies

I’m trying to make a spectator script, but my script is unable to detect when I have died. I think the problem is that by the time the humanoid dies its deleted too fast. And I don’t have a way to figure that out.

local players = game:GetService('Players')
local localplayer = players:WaitForChild('VonickTW')
local teleportpart = workspace.SpectatorTeleport
-- ADD HERE
while wait(0.001) do
	character = localplayer.Character
end

if character.Humanoid.health == 0 then
	wait(5)
	character.HumanoidRootPart.Position = Vector3.new(-70.25, 5.75, -472.5)
	print("Telported | Position = "..character.Position)
end
2 Likes

the code next to a While loop will not run until the While have done, instead of while use repeat as

repeat wait()
until localplayer.Character

character = localplayer.Character

About the next code it will not run because the If Statement will only run once, you have to do an event :

character:WaitForChild("Humanoid").Died:Connect(function()
-- Code goes here
end)

Even with this, the code will only run if the humanoid dies for the first time so ill recommend to use a server script that detects when a character is added and then if the character´s humanoid dies, runs your code

1 Like

I’ve updated your script and played with different variations and it works great. Except for the fact that the character is not moving without a console error.

Heres the script:

local players = game:GetService('Players')
local localplayer = players:WaitForChild('VonickTW')
local teleportpart = workspace.SpectatorTeleport
-- ADD HERE
repeat wait()
until localplayer.Character

character = localplayer.Character

character:WaitForChild("Humanoid").Died:Connect(function()
	wait(4.5)
	character.HumanoidRootPart.Position = teleportpart.Position
	print('Moved Position')
end)

Replace localplayer’s code with players.LocalPlayer and just do

localplayer.CharacterAdded:Connect(function(Character)
  Character:WaitForChild("Humanoid").Died:Connect(function()
    --code here
  end)
end

you should also use task.wait() instead of wait()

1 Like

This still isn’t working. I’m not really sure what switching would fix. I would still be getting the same output but with a different way to get it. Since this is a server script. PlayerAdded() Makes more sense.

Can you successfully getting ‘Moved Position’ text? I couldn’t understand what did you meant.

Yes I can get the print on the console

This is a very inefficient and ineffective way to check if a Player has died or not,
Some code provided are actually very inefficient, plus useless in such cases, which they can easily be swapped out for more Efficient and Resourceful methods, instead of repeat until Character, you can simply do:

local Character = Player.Character or Player.CharacterAdded:Wait() -- (nil or Model, Model or Model)

Which is a much better way to get the Character in my opinion.
As the other mentions, you should be using Humanoid.Died to detect if the Player has died or not. and if statement may not catch this change and will be skipped.
And a while loop isn’t going to help you in anyway at all here:

  1. It take s a LOT of resources to have it run, Although they are efficient, they Update the code every single time instead of having the code listen for changes, which is a downside, and can cause lag, Listening for Changes it much more efficient than a loop running constantly.

  2. It wont have any code below it run at all, as its prioritizing the loop within the thread, if youw ant it to run alongside the main thread, use a coroutine or task for this usage.

I see these as downsides, you can let me know what you think however.


Another thing is that If you are going to be Moving the Character to a Position, you should be using Model:MoveTo, or Model:PivotTo to move them to a set Location.

Now, you may be confused, “wait, isn’t MoveTo used to make the Character Move?”, yes and no., The Model Instance as a Function called MoveTo, but unlike Humanoid:MoveTo, it teleports the Character (or Instances) to a set Location.

PivotTo does the exact same thing as MoveTo with minor differences, its bacially the bettwe version of its predecessor: SetPrimaryPartCFrame.

Character:MoveTo(Vector3Here) -- Moves using Vector3
Character:PivotTo(CFrameHere) -- Moves using CFrame (better version of SetPrimaryPartCFrame)

There are so many methods to choose from, and yet you pick the longer, and more resource intensive ones.

Instead what you should do this:

  • Use the code for the Character I provided

  • Use the Humanoid.Died Event to detect the Player on death

2 Likes

I was aware of the inefficiency of this script and I was afraid it might be my only option. Thank you for the suggestions. But you didnt have to right a whole essay bro :skull:.

local players = game:GetService('Players')
local localplayer = players:WaitForChild('VonickTW')
local teleportpart = workspace.SpectatorTeleport
-- ADD HERE
local Character = localplayer.Character or localplayer.CharacterAdded:Wait() -- (nil or Model, Model or Model)


localplayer.CharacterAdded:Connect(function(Character)
	Character:WaitForChild("Humanoid").Died:Connect(function()
		task.wait(4.5)
		repeat until Character
		Character:MoveTo(-70.25, 5.75, -472.5)
		print('Moved Position')
	end)
end)

Have I done this correctly?

This part is a bit unnecessary as you already have the Character Beforehand, so you should remove CharacterAdded

What is the purpose of this? The Character already exists but in a Dead State?

You have to use Vector3.new().

local Character = localplayer.Character or localplayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.Died:Connect(function()
    task.wait(4.5) -- waits 4.5 seconds
    
    Character:MoveTo(Vector3.new(-70.25, 5.75, -472.5)) -- Moves Model using Vector3
    print('Moved Position') -- for Debugging
end)

1 Like

instead of using a loop to wait until theres a character, do this instead

local character = localplayer.Character or localplayer.CharacterAdded:Wait()

and to detect when the player has died, you can do

local humanoid = character:WaitForChild("Humanoid")
humanoid:GetPropertyChangedSignal("Health"):Connect(function()
        if humanoid.Health <= 0 then
            character = player.CharacterAdded:Wait()
	        character:MoveTo(Vector3.new(-70.25, 5.75, -472.5))
        end
end)
1 Like

Bro?


Humanoid.HealthChanged
Seriously.

1 Like

I’ve tampered the script slightly and it works fully now! Thanks for the help!

local players = game:GetService('Players')
local localplayer = players:WaitForChild('VonickTW')
local Character = localplayer.Character or localplayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.Died:Connect(function()
	wait(4)
	local Character = localplayer.Character or localplayer.CharacterAdded:Wait()
	Character:MoveTo(Vector3.new(-70.25, 5.75, -472.5)) -- Moves Model using Vector3
	print('Moved Position') -- for Debugging
end)

Heres the final product

no hablo ingles bueno lo siento

1 Like

Puedo hablar espanol.

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