Description: When 2 players touch a part (in the same server or different) they get added in a data store and teleported in a private server of the same place. Idk how to do that bacause im a noob pls help. (I used AI to make this)
Inside-part Local Script:
local part = script.Parent
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local groupStore = DataStoreService:GetDataStore("TeleportGroups")
part.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end
-- Add player to group
pcall(function()
groupStore:UpdateAsync("ActiveGroup", function(current)
current = current or {}
if not table.find(current, player.UserId) then
table.insert(current, player.UserId)
end
return current
end)
end)
end)
ServerScriptService:
local DataStoreService = game:GetService("DataStoreService")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local groupStore = DataStoreService:GetDataStore("TeleportGroups")
local function attemptTeleport()
pcall(function()
local group = groupStore:GetAsync("ActiveGroup") or {}
if #group >= 2 then
-- Reserve new private server
local serverCode = TeleportService:ReserveServer(game.PlaceId)
-- Find players in the current server
local targets = {}
for _, userId in ipairs(group) do
local player = Players:GetPlayerByUserId(userId)
if player then
table.insert(targets, player)
end
end
-- Clear group AFTER reserving server
groupStore:SetAsync("ActiveGroup", {})
-- Teleport if we have 2 valid players
if #targets >= 2 then
TeleportService:TeleportToPrivateServer(
game.PlaceId,
serverCode,
targets
)
end
end
end)
end
-- Check every 3 seconds
while true do
task.wait(3)
attemptTeleport()
end
-- Cleanup when players leave
Players.PlayerRemoving:Connect(function(player)
pcall(function()
groupStore:UpdateAsync("ActiveGroup", function(current)
if current then
table.remove(current, table.find(current, player.UserId))
end
return current
end)
end)
end)
Sorry guys i use AI to learn faster but i really wanna make this thing i promise i’ll stop using it as time passes and i learn more scripting