I’m trying to teleport a character when they press a button but it is inconsistent. This is an issue that I have had for the past year on a few different games. I’m wondering how I can solve it.
The issue is the code runs fine but it just doesn’t actually teleport the player.
Usually this occurs when the player is experiencing a lag spike and has poor WiFi. For me it probably occurs 1 every 500 times but for other players it seems to occur more often and I get lots of reports of it.
I’ve tried deferring the teleport with task.defer. I’ve also tried using a repeat loop until the player successfully teleports. Something I noticed when using the repeat loop is that if you print the characters position immediately after you update their cframe it will give you the new value so I had to add a short wait. Although the repeat loop worked, it felt a bit hacky.
Here is my code:
local character = player.Character
local map = Map.GetMap()
local spawns = map:FindFirstChild("Spawns")
if not spawns then warn("MAP HAS NO SPAWNS") return end
local spawnPoints = spawns:GetChildren()
local teleportTarget = spawnPoints[math.random(1, #spawnPoints)].Position
player:RequestStreamAroundAsync(teleportTarget)
character:PivotTo(CFrame.new(teleportTarget))
Although this is using streaming enabled, I’ve also had this occur on games where I don’t use streaming enabled.