Skip to content

Instantly share code, notes, and snippets.

@acs
Last active December 6, 2023 13:14
Show Gist options
  • Save acs/6565a66f278c000f76cc1a76b98b3494 to your computer and use it in GitHub Desktop.
Save acs/6565a66f278c000f76cc1a76b98b3494 to your computer and use it in GitHub Desktop.
Code to teleport players if there are enough players in the lobby (not completed yet)
-- Script to teletransport the player to another place (Capitulo 1)
local CAPITULO_1_PLACE_ID = 15504750119
local MIN_PLAYERS_TO_TELEPORT = 2
local playersReadyToTeleport = {}
local teleportPlace = script.Parent
local world = teleportPlace.Parent
local numPlayersReadyBanner = world.Text
local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local TARGET_PLACE_ID = CAPITULO_1_PLACE_ID
local function extractNumPlayers(numPlayersText)
local numPlayersReadyText = numPlayersReadyBanner.CustomPlayerTag.PlayerName.Text;
print('Players ready: ', numPlayersReadyText)
return tonumber(numPlayersReadyText:sub(0,1))
end
local function addPlayerToTeleport(player)
table.insert(playersReadyToTeleport, player)
numPlayersReadyBanner.CustomPlayerTag.PlayerName.Text = tostring(#playersReadyToTeleport) .. "/" .. tostring(MIN_PLAYERS_TO_TELEPORT) ;
end
local function teleportPlayers(listOfPlayers)
for pos, player in pairs(listOfPlayers) do
TeleportService:TeleportAsync(TARGET_PLACE_ID, {player})
end
end
local function findPlayerFromPart(part:Part)
-- internal part like the handle
local model = part.Parent.Parent
local player = Players:GetPlayerFromCharacter(model)
if not player then
-- external part like the right hand
model = part.Parent
player = Players:GetPlayerFromCharacter(model)
end
return player
end
local function onTouched(partTouched)
print(teleportPlace.Name .. " collided with " .. partTouched.Name)
local posPart = partTouched.Position
local Mag = (partTouched.Position - teleportPlace.CFrame.Position).Magnitude
local newPlayer = findPlayerFromPart(partTouched)
if not table.find(playersReadyToTeleport, newPlayer) then
addPlayerToTeleport(newPlayer)
else
return
end
local numPlayersReady = extractNumPlayers()
-- If there are enough players, teleport them
if (numPlayersReady >= MIN_PLAYERS_TO_TELEPORT) then
local playerToTeleport = Players:GetPlayers()[1] -- get the first user in the experience
local playersToTeleport = {playerToTeleport}
teleportPlayers(playersToTeleport)
else
print('Not enough players to teleport', numPlayersReady)
end
end
local function init()
numPlayersReadyBanner.CustomPlayerTag.PlayerName.Text = tostring(#playersReadyToTeleport) .. "/" .. tostring(MIN_PLAYERS_TO_TELEPORT) ;
end
init()
teleportPlace.Touched:Connect(onTouched)
@acs
Copy link
Author

acs commented Dec 6, 2023

Just pending to check that players are inside the lobby does not get out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment