PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

TeleportService: Teleport to a Reserved Server via Chat

TeleportService: Teleport to a Reserved Server via Chat

Oct 01 2018, 1:42 AM PST

The following code would reserve one server, if it hasn’t be reserved before. Whenever someone says “reserved” he/she will be teleported to the private server.

local TS = game:GetService("TeleportService")
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetGlobalDataStore()
 
-- Get the saved code
local code = DS:GetAsync("ReservedServer")
if type(code) ~= "string" then -- None saved, create one
	code = TS:ReserveServer(game.PlaceId)
	DS:SetAsync("ReservedServer",code)
end
 
local function Joined(plr)
	-- Everytime they chat, we want to know
	plr.Chatted:Connect(function(msg)
		if msg == "reserved" then -- Aha, that's our cue
			TS:TeleportToPrivateServer(game.PlaceId,code,{plr})
		end
	end)
end
 
-- Connect all current and future players
Players.PlayerAdded:Connect(Joined)
for k,v in pairs(Players:GetPlayers()) do
	Joined(v)
end