Last active
December 6, 2023 13:14
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just pending to check that players are inside the lobby does not get out