But, when the character dies, for some reason the script cannot find the HumanoidRootPart (i’m using waitforchild) when the respawntime is 0 after death, and if the player dies mid-script the script will throw an error.
Does anyone know:
What is causing the HumanoidRootParts not to be found
How to immediately break from a loop when the HumanoidRootPart is not found
local playerService = game:GetService("Players")
for i, v in pairs(playerService:GetPlayers()) do
if v.Character then
local hrt = v.Character:WaitForChild("HumanoidRootPart")
end
end
This is a bit of speculation, but I see no reason why that wouldn’t work. Other than a few moments, it wouldn’t exist. You’d have to deal with that where it is being used in the script, too.
I am a bit confused with what you are trying to achieve.
If you are trying to keep track of all hrps in-game, here’s how you do it:
(server-side script)
local allHrps = {}
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
table.insert(allHrps, char.PrimaryPart)
end)
player.CharacterRemoving:Connect(function(char)
for _, hrp in allHrps do
if not hrp.Parent then
table.remove(allHrps, table.find(allHrps, hrp))
end
end
end)
end)
Great, thanks! This does work in finding the hroots, but if a script was running, how would you immediately break from the loop if the specific char died (player.characterRemoving) (I am looping on all chars)?
For instance if in the middle of the loop the hroot gets removed and we try to get its position it will throw an error since it doesn’t exist.
You can use if hrp.Parent then which checks if the HRP still exists.
Check it once when the loop for that hrp starts. If it goes through, you are good for that HRP.
Note that if you have any yields, such as wait(), you should check it again after the yield.
if hrp.Parent then
-- do stuff
wait(1)
if hrp.Parent then
-- do stuff
else
-- HRP no longer exists
end
else
-- HRP does not exist
end
Unfortunately, I already tried that and it doesn’t work for this reason:
I have created a picture to show what I mean: the humanoid exists when the if statement runs, but halfway through it no longer exists.
Is there a way to restart the loop and get new chars without throwing an error?
To be honest, you could just parent a script to the startercharacterscripts and then just get the humanoidrootpart by doing script.Parent:WaitForChild(“HumanoidRootPart”) and this will be done each time the character spawns (dies).
Local Script right? Need it to be server side :(. All I really need is the position of the hroot but people could exploit that so I would need to double check that with the server script: but the player could die before the script finishes, and that would through an error.
I mean how can you run in parallel something that can check if the player’s parent is nil, and then stop the loop to make sure that the player doesn’t die before the script can check if it has to break. Check message 13 please for explanation
playerService.PlayerAdded:Connect(function(plr)
table.insert(players, plr)
local character = plr.CharacterAdded:Wait()
if character then -- can check for if they exist by this being true.
table.insert(characters, character)
end
end)
How is it possible for them to die within the second they respawn? Might want to think about adding time to that bubble or spawning them in a safe spot. Maybe you’re dealing with this problem in the wrong spot.