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