Infinite yield on present child

I am making a script so that you teleport to the associated checkpoint when you spawn in. I have a script under the player. And a folder filled with the checkpoints that have numbers as names. Checkpoints will randomly not work and then work. What should I do?

local replicatedstorage = game:GetService('ReplicatedStorage')
local teleport = replicatedstorage.Remotes.TeleportRemote
local players = game:GetService('Players')
local player = players.LocalPlayer
local leaderstats = player:WaitForChild('leaderstats')
local checkpointvalue = leaderstats:WaitForChild('Checkpoint')

local function TpToCheckpoint(char)
	if checkpointvalue.Value ~= 1 then
		local checkpoint = workspace.Stages:WaitForChild(checkpointvalue.Value)
		teleport:FireServer(char:WaitForChild('HumanoidRootPart'), checkpoint:WaitForChild('Union').Position + Vector3.new(0,4,0),checkpoint.Configuration.Orientation.Value)
	end	
end

TpToCheckpoint(player.Character or player.CharacterAdded:Wait())
player.CharacterAdded:Connect(TpToCheckpoint)

First of all, you should tell us which of the following lines/paths is causing the yield:

  1. player.leaderstats
  2. leaderstats.Checkpoint
  3. workspace.Stages[checkpointvalue.Value]
  4. char.HumanoidRootPart
  5. checkpoint.Union

At least give us the yield message so we can figure it out.

Also since this is related to the character, not the player, you can change the script location to StarterCharacterScripts to simplify the script:

--Make it a script instead of a local script
local character = script.Parent
local player = game.Players:GetPlayerFromCharacter(character)

local checkpoint = player:WaitForChild("leaderstats"):WaitForChild("Checkpoint")

if checkpoint.Value == 1 then return end

local spawnPart = workspace.Stages:WaitForChild(checkpoint.Value)
local root = character:WaitForChild("HumanoidRootPart")
local pos = spawnPart:WaitForChild("Union").Position+Vector3.new(0,4,0)
local rot = spawnPart.Configuration.Orientation.Value

--Assuming Orientation to be a Vector3
local r = math.rad
root.CFrame = CFrame.new(pos)*CFrame.Angles(r(rot.X), r(rot.Y), r(rot.Z))
1 Like

Checkpoint.Union is causing the problem
image

1 Like

Ok i just tested the game and all the unions from checkpoint 28+ are gone. But when im editing the game they are there inside the object

Your current checkpoint doesn’t have a union part, also why do you need that to calculate position, can’t you just do spawnPart.Position instead?

1 Like

Calculating the position with the object doesnt work.

As long the spawnPart isn’t moving and doesn’t change sizes and such between stages, you can just offset the result to make it work(as you already do for height by adding 4).

1 Like

So i wouldnt attach it to the checkpoints current position?

I actually had this problem a lot before when making my game, What i did to fix it was make the parts higher up, because in my case, they would get deleted because they were too far from spawn, therefore making them deleted in the void. Sorry if this does not solve your problem, but its worth a try!

1 Like

I just figured this out! I was worried that was a problem because the parts were so far. So how do i change the render distance?

1 Like

No need to change the render distance, Just make the parts’ CFrames closer to the middle of the map (middle being 0,0,0)

1 Like

Ok will do! Thank you so much. I’ll update you when it fixes

1 Like

Oh, and Make sure they are anchored, if not, that could possibly be another reason why they get deleted (because they fall into the void)

1 Like

I made sure they were anchored yes.

1 Like

My friend doesnt want to move everything can i just use a module script?

1 Like

Could you provide some more details about the union part?

What size is the union? (like big, or small)
What would you have to move?
If the union doesn’t have to been seen or collided with, Then it shouldn’t exactly matter where its positioned at.

The union part is just an almost flat checkpoint. And its not very big. I have to move the player to it when spawning in. And the union does need to be seen when close enough and collided with.

1 Like

Is there anything wrong with having it at 0,0,0 in studio? and then when the character respawns, move it there, that way, i dont think it should be deleted since it doesnt spawn in the void.

1 Like

I think im failing to understand. do I move the character there? The problem isnt its spawning in the void. Its just out of render distance. And this is an obby

1 Like

Alright, Then move the union closer to where ever players spawn, So it wont be deleted. Also, I meant move the character to the union, Not 0,0,0. Sorry for not specifying.

1 Like