So, I have a round-based game, and I have been experimenting with teleportation methods to get players from the lobby into the arena. I want to know if this teleportation method seems optimized enough or is reliable, as I plan to release my game soon.
local SpleefSpawns = game.Workspace:WaitForChild("SpleefSpawns"):GetChildren()
-- This model named SpleefSpawns contains invisible bricks used as spawn points.
for _, player in pairs(game.Players:GetPlayers()) do
if player and #SpleefSpawns > 0 and game:GetService("ServerStorage"):WaitForChild("Players")[player.Name].isAFK.Value == false then
local playerRoot = player.Character:WaitForChild("HumanoidRootPart")
local allSpleefSpawns = math.random(1, #SpleefSpawns) -- Choses a random spawn point for the player
local randomSpawn = SpleefSpawns[allSpleefSpawns]
if randomSpawn and playerRoot then
game.ReplicatedStorage:WaitForChild("AFKRound"):FireClient(player, "InRound")
table.remove(SpleefSpawns, allSpleefSpawns) -- Removes the spawn that player was sent to from the choosing table so another player doesn't spawn there as well
playerRoot.Anchored = true
playerRoot.CFrame = CFrame.new(randomSpawn.Position + Vector3.new(0, 1, 0))
end
end
end
wait(2) -- Holds the player in the air for 2 seconds
for _, player in pairs(game.Players:GetPlayers()) do
if player and game:GetService("ServerStorage"):WaitForChild("Players")[player.Name].isAFK.Value == false then
local playerRoot = player.Character:WaitForChild("HumanoidRootPart")
playerRoot.Anchored = false
end
end
Thank you for any feedback you provide on this teleportation system
Good code!
However I do think you can make better use of variables; there’s times you indexed game.ReplicatedStorage where you could make a variable to replace that named as RepStor .
You could use :MoveTo() instead of anchoring the root part and changing the CFrame of it.
Not sure why you need all of the players to be held in the air for 2 seconds however.
Not sure if you’ve thought about if the player was to reset when this code is running?
To solve that issue you could have a .CharacterAdded event which can just check if the player is supposed to be in the game and teleport them into the spawn they were supposed to be in.
I like this alot, it’s simple, well-coded, and easy to read.
I’ve used lua-inspect to review your code for any memory leaks, unused variables, etc and turns out you have no issues!
However, I’d advise to use :MoveTo instead of the old-fashioned CFrame
Also, if you want the extra performance gain you should divide stuff like game:GetService("ServerStorage"):WaitForChild("Players")[player.Name].isAFK.Value
in locals so that they aren’t repeatedly being indexed as globals.
Thank you so much for your feedback! I have been using :MoveTo() recently migrating away from the CFrame method, again thanks and I’ll be sure to divide that up in variables.
This is nice! I also set the HumanoidRootPart CFrame. Now, however, I tween their HumanoidRootPart so that they “fly” to the game. It goes for a great cartoony effect, if that’s what your game is styled as!