Here’s an example (that I have not tested) that does most of what I talked about:
local userIdJoiningReservedServerIdStore = game:GetService('DataStoreService'):GetDataStore('UserIdJoiningReservedId')
local jobIdToReservedServerIdStore = game:GetService('DataStoreService'):GetDataStore('JobIdToReservedServerId')
local function getTeleportInfoForUsername(username)
local userId = game.Players:GetUserIdFromNameAsync(username)
if not userId then
return false, 'User by name '..username..' does not exist. Did you type the name wrong?'
end
local success, errorMsg, placeId, jobId = game['Teleport Service']:GetPlayerPlaceInstanceAsync(userId)
if not success then
return false, errorMsg
end
local reservedServerId = jobIdToReservedServerIdStore:GetAsync(jobId)
if not reservedServerId then
return true, 'Success', {
reservedServerId = nil,
placeId = placeId,
jobId = jobId
}
end
return true, 'Success', {
reservedServerId = reservedServerId,
placeId = placeId,
jobId = jobId
}
end
local function teleportPlayerToReservedServer(player, reservedServerId, placeId)
userIdJoiningReservedServerIdStore:SetAsync(tostring(player.UserId), {
reservedServerId = reservedServerId,
placeId = placeId,
time = os.time()
})
game['Teleport Service']:TeleportToPrivateServer(placeId, reservedServerId, {player})
end
local function teleportPlayerToUsername(player, username)
local success, errorMsg, teleportInfo = getTeleportInfoForUsername(username)
if not success then
return errorMsg
end
if teleportInfo.reservedServerId then
teleportPlayerToReservedServer(player, teleportInfo.reservedServerId, teleportInfo.placeId)
else
game['Teleport Service']:TeleportToPlaceInstance(teleportInfo.placeId, teleportInfo.jobId, player)
end
end
local function isReservedServer()
return game.VIPServerId ~= '' and game.VIPServerOwnerId == 0
end
local myReservedServerId
local function onPlayer(player)
if isReservedServer() and not myReservedServerId then
local now = os.time()
local reservedServerInfo = userIdJoiningReservedServerIdStore:GetAsync(tostring(player.UserId))
if reservedServerInfo and now - reservedServerInfo.time < 2*60 and reservedServerInfo.placeId == game.PlaceId then
myReservedServerId = reservedServerInfo
jobIdToReservedServerIdStore:SetAsync(game.JobId, myReservedServerId)
end
end
userIdJoiningReservedServerIdStore:RemoveAsync(tostring(player.UserId))
end
game.Players.PlayerAdded:Connect(onPlayer)
for _, player in next, game.Players:GetPlayers() do
spawn(function()
onPlayer(player)
end)
end
You would be able to use teleportPlayerToUsername(player, username)
to teleport player
to the user with username username
.
This is only an example. You should learn from this example and implement it yourself. This example skips over some very important things like error checking in order to get across what it’s doing better,