Hey guys.
I’m sure I’m overlooking something here, because this should be simple stuff.
I have a method in a ModuleScript which is used to teleport the character. I am using SetPrimaryPartCFrame because at the time of making this code, SetPrimaryPartCFrame was not deprecated. Correct me if I am wrong, but even though it is obsolete, it should still theoretically work and therefore isn’t the source of the issue.
The method works 99% of the time. I’ve only seen a few cases where the player did not teleport, and I have yet to figure out where I fail to catch the condition in my method. In all cases, none of the players were sitting, nor were they dead or respawning. They were simply just standing there, which makes this case even more puzzling.
Am I not waiting for something properly? That’s usually the cause for inconsistent code.
There are no errors in the console when this bug occurs.
Here is the method:
function characterService:TeleportPlayerToCFrame(player, cf)
spawn(function()
local c = player.Character or player.CharacterAdded:Wait() --Get character or yield wait to retrieve it
c:WaitForChild("Humanoid") --Ensure the humanoid is a child of character
if c.Parent ~= workspace then
repeat RunService.Heartbeat:Wait() until c.Parent == workspace --Check to make sure character is in workspace
end
if c.Humanoid.Sit then --If the player is sitting
c.Humanoid.SeatPart.SeatWeld:Destroy()
repeat RunService.Heartbeat:Wait() until c.Humanoid.Sit == false
end
if c.Humanoid.Health <= 0 then return end --Don't teleport if dead
c:SetPrimaryPartCFrame(cf)
end)
end
Any feedback would be appreciated.
Thanks