:LoadCharacter() sending player to spawn location even with functions to prevent that

repeat
task.wait(0.1)
until script.Parent.Parent:FindFirstChild(“UpperTorso”) and script.Parent.Parent:FindFirstChildOfClass(“Humanoid”)

local character = script.Parent.Parent
local upperTorso = character:FindFirstChild(“UpperTorso”)
local humanoid = character:FindFirstChildOfClass(“Humanoid”)
local connection
if upperTorso then
script.Parent.Parent = upperTorso
end

if humanoid then
humanoid:GetPropertyChangedSignal(“Health”):Connect(function()
script.Parent.Enabled = humanoid.Health <= 0
connection = game[“Run Service”].Heartbeat:Connect(function()
humanoid:UnequipTools()
end)
if humanoid.Health > 0 then
connection:Disconnect()
end
script.Parent.Triggered:Connect(function(p)
if p.Character:FindFirstChild(“Bandage”):IsA(“Tool”) or p.Character:FindFirstChild(“Medkit”):IsA(“Tool”) then
local plr = game.Players:GetPlayerFromCharacter(character)
if plr then
plr:LoadCharacter()
task.defer(function()
plr.Character:PivotTo(plr.Character.HumanoidRootPart.CFrame)
end)
p.Character:FindFirstChildOfClass(“Tool”):Destroy()
end
end
end)
end)
end

This is my current code for my reviving system in my survival game, as you can see, there isn’t really anything that I am mad about, but there is something that could be set aside that I kinda take a bit seriously, because when using :LoadCharacter() on a player, it usually sets the position of the respawned player to the spawn location, and I don’t want that, so I used task.defer() from an other post, but for some reason, that didn’t work either with no output logs, so I would like recommendations on how to fix this
(Edit to prevent the whole post from being in code. so don’t mention anything about the code not having spaces before the code line)

1 Like

I believe you need to reference the old Character’s CFrame before loading their Character.
To me this just seems you are respawning them and then setting their CFrame to their Character which has already been respawned.

(I have not used Task.Defer so if I am incorrect, please do correct me.)


put up exactly what you prefered, though it still doesn’t quite work, like nothing had changed in the code

Hm… Only other thing I can think of is putting a task.wait() just before setting the CFrame.

Try this instead:

local cframe = plr.Character:WaitForChild("Head", 5).CFrame
plr:LoadCharacter()
task.defer(function()
  if cframe == nil then return end 
  task.wait()
  plr.Character:PivotTo(cframe)
end)
1 Like

I’m assuming because of the line script.Parent.Triggered that this script is inside a ProximityPrompt? Also, task.defer( function ) allows you to run code inside the passed function without holding up the code below.

There’s a few problems here:

  1. When you call plr:LoadCharacter(), the character model is immediately deleted, including the script that this code is running from. So, the code immediately below this will never run.
  2. You then try to set the player character’s CFrame to its own current CFrame - so nothing would really happen if it did run. plr.Character:PivotTo(plr.Character.HumanoidRootPart.CFrame) - note using the exact same variable ‘plr’, this is just the same player object!

To fix this, when it comes to re-positioning the character, you’ll need to run code from a script that isn’t inside the character.
Then, you’ll need to store a variable of the character’s CFrame when the humanoid dies, and after respawning the character, pivot them to that variable.

-- Put this code inside your script to track the last death position
local deathPoint = nil
humanoid.Died:Connect(function()
	deathPoint = character.HumanoidRootPart.CFrame
end

Merge the code below into what you showed in the OP

local plr = game.Players:GetPlayerFromCharacter(character)
script.Parent.Triggered:Connect(function(otherPlayer)
	if  --[[ check for healing items here ]] then
		if plr then
			script.Parent = game.Workspace -- avoid the script being deleted when we re-load
			-- ^^ i really wouldn't recommend doing this way but it works as a temporary measure.
			plr:LoadCharacter()
			if deathPoint then
				plr.Character:PivotTo(deathPoint)
				deathPoint = nil
			else
				warn("deathPoint was nil! could not teleport me.")
			end
			-- then delete the other player's healing item
			otherPlayer.Character:FindFirstChildOfClass("Tool"):Destroy()
			-- Since we re-parented the script outside of the character, we need to delete it.
			-- otherwise countless useless scripts would fill the workspace and never disappear!
			script:Destroy()
		end
	end
end)

Additionally, instead of using repeat task.wait(0.1) until blablabla:FindFirstChild(“UpperTorso”), use:
local upperTorso = character:WaitForChild("UpperTorso")
The script will wait at this line of code until the thing you’re finding is found and then continues.

Another bonus tip for showing code on the forums: type three backticks ``` (if you have a british keyboard then it’s the top left key next to [1]) then enter a new line and enter another three backticks, it will format your text inbetween to appear like my code above :grinning: