Hello! I have been tinkering around with TeleportService lately when I noticed something weird. I have this local script that checks if the suer has followed anyone into the game, which then asks the server if that person is still in the game and in a private server. If true, then the game accesses the reserved code from the data store, and teleports the person to the private server their friend is in. It seems to work, and has no errors, but when I actually test it (in-game), it seems to make a new private server for no reason. Would any of you know why this is the case? I will list the code below so you can help.
Local Script:
local rs = game:GetService("ReplicatedStorage")
local gameEvents = rs:WaitForChild("GameEvents")
local teleporting = gameEvents:WaitForChild("Teleporting")
local checkForJoin = teleporting:WaitForChild("CheckForJoin")
local plr = p.LocalPlayer
local joinedPlayerId = plr.FollowUserId
if joinedPlayerId > 0 then --if none it defaults to 0, not nil
checkForJoin:FireServer(joinedPlayerId)
end
Server Script:
local tps = game:GetService("TeleportService")
local rs = game:GetService("ReplicatedStorage")
local dss = game:GetService("DataStoreService")
local gameEvents = rs:WaitForChild("GameEvents")
local teleporting = gameEvents:WaitForChild("Teleporting")
local checkForJoin = teleporting:WaitForChild("CheckForJoin")
checkForJoin.OnServerEvent:Connect(function(plr, joinedPlayerId)
local success, _, errorMsg, placeId, jobId = pcall(function()
return tps:GetPlayerPlaceInstanceAsync(joinedPlayerId)
end)
if success then
local reservedCodes = dss:GetDataStore("ReservedServerCodes")
local joinedCode = reservedCodes:GetAsync(joinedPlayerId)
print(joinedCode)
task.wait(5) --this will be removed, it just gives me enough time to press f9
if joinedCode then
tps:TeleportToPrivateServer(placeId, joinedCode, {plr})
end
else
print(errorMsg)
end
end)
Here is also the local and server scripts for being the first person to join the private server (the person who created)
Local Script:
local rs = game:GetService("ReplicatedStorage")
local gameEvents = rs:WaitForChild("GameEvents")
local teleporting = gameEvents:WaitForChild("Teleporting")
local teleportPlayer = teleporting:WaitForChild("TeleportPlayer")
local menuFrame = script.Parent
local menuGui = menuFrame.Parent
local plrGui = menuGui.Parent
local gameGui = plrGui:WaitForChild("GameGui")
local cabinButton = menuFrame:WaitForChild("CabinButton")
local plr = p.LocalPlayer
cabinButton.MouseButton1Click:Connect(function()
local placeId = 125298753777657
saveDataReal:FireServer()
teleportPlayer:FireServer(placeId, plr, {playerData, ["WasSent"] = true})
end)
Server Script:
local tps = game:GetService("TeleportService")
local rs = game:GetService("ReplicatedStorage")
local dss = game:GetService("DataStoreService")
local gameEvents = rs:WaitForChild("GameEvents")
local teleporting = gameEvents:WaitForChild("Teleporting")
local teleportPlayer = teleporting:WaitForChild("TeleportPlayer")
teleportPlayer.OnServerEvent:Connect(function(_, placeId, plr, info)
local reservedCodes = dss:GetDataStore("ReservedServerCodes")
local reservedCode = tps:ReserveServer(placeId)
reservedCodes:SetAsync(plr.UserId, reservedCode)
local joinedCode = reservedCodes:GetAsync(plr.UserId)
print(joinedCode)
task.wait(5)
tps:TeleportToPrivateServer(placeId, reservedCode, {plr}, nil, info)
end)
Is there anything that could be wrong? Maybe I used the wrong key and it’s making a new private server based on a code that didn’t exist before? I seriously don’t really know what it could be.