How do i fix this teleporting script

anyone know how to solve

for i,v in ipairs(game.Players:GetPlayers()) do
    if target == tostring(v.UserId) then
        print("teleporting")
        local code = tostring(CodeID)                
        TS:TeleportToPrivateServer(game.PlaceId,code,v)  
    end
end

i get unable to cast values to object error and when i use

for i,v in ipairs(game.Players:GetPlayers()) do
    if target == tostring(v.UserId) then
        print("teleporting")
        local code = tostring(CodeID)    
        local players = game.Players:GetPlayers()            
        TS:TeleportToPrivateServer(game.PlaceId,code,players)  
    end
end

i get error 769 and it fails from teleporting the player

all i want to is to teleport a single player to a specfic reserved server but it gives me that unable to cast values to object error and when i make it get all the players in the server and teleport them it gives me error 769

1 Like

put the target into a table for this.

for i,v in ipairs(game.Players:GetPlayers()) do
    if target == tostring(v.UserId) then
        print("teleporting")
        local code = tostring(CodeID)                
        TS:TeleportToPrivateServer(game.PlaceId,code,{v})  
    end
end
2 Likes

that was a good solution for the issue of teleporting a single player but im still having the error 769 issue that prevents me from teleporting do you know a solution for it

1 Like

this would probably because you are trying to teleport them with a string. Use ReserveServer()

for i,v in ipairs(game.Players:GetPlayers()) do
    if target == tostring(v.UserId) then
        print("teleporting")
        local code = TS:ReserveServer(CodeID)                
        TS:TeleportToPrivateServer(game.PlaceId,code,{v})  
    end
end

Sorry for the late response btw

1 Like

does this script make a reserveserver or make them join into one cuz im trying to make them join into a reserve server

1 Like

it reserves the server for the game.

1 Like

the server it self is a reserved server thats already made and im trying to send the players there

1 Like

then why are you using tostring for the Code? just use the reserved server without the tostring. If it helps look at this forum.

1 Like

all i tried to is to send the code from the reserved server to another one (each one has 1 player) and when i press a button its supposed to teleport the player from the other server to the current server im in using the code i tried using the code normally still the same issue

1 Like

imma try to check if the code is written correctly

1 Like

Can I see your whole script??.

1 Like

Are you trying to teleport in studio?

this sends from a client to server and then sends the values in a messaging service


request.OnServerEvent:Connect(function(player, Target)
	local value = Target..game.PrivateServerId..string.len(Target)
	player.Character.Config.Requests.IsAccepting.Value = true
	print(game.PrivateServerId)
	MessagingService:PublishAsync(Topic2,value)
end)

MessagingService:SubscribeAsync(Topic2, function(Message)
			print("detected")
			if player.Character.Config.Requests.IsAccepting.Value == false then
				local value = Message.Data
				local lastdigit = value:sub(#value, #value) -- detects how many characters is the player so it can differenciate between the code and player
				local TargetID = value:sub(1, lastdigit) -- detects the player
				local CodeID = value:sub(lastdigit + 1, #value - 1) -- detects the code
				local target = tostring(TargetID)
				print(CodeID)
				for i,v in ipairs(game.Players:GetPlayers()) do
					if target == tostring(v.UserId) then
						local players = game.Players:GetPlayers()
						print("teleporting")
						TS:TeleportToPrivateServer(game.PlaceId,CodeID,{v})
						--TS:TeleportToPrivateServer(game.PlaceId,code,players)
					end
				end
			end
		end)```

nope live game im trying with my phone and pc

just do

MessagingService:SubscribeAsync(Topic2, function(Message)
			print("detected")
			if player.Character.Config.Requests.IsAccepting.Value == false then
				local value = Message.Data
				local lastdigit = value:sub(#value, #value) -- detects how many characters is the player so it can differenciate between the code and player
				local TargetID = value:sub(1, lastdigit) -- detects the player
				local CodeID = value:sub(lastdigit + 1, #value - 1) -- detects the code
				local target = tostring(TargetID)
				print(CodeID)
				for i,v in ipairs(game.Players:GetPlayers()) do
					if target == tostring(v.UserId) then
						local players = game.Players:GetPlayers()
						print("teleporting")
						local code = TS:ReserveServer(game.PlaceId) --this will be the reserved server code
						TS:TeleportToPrivateServer(game.PlaceId,code,{v})
						--TS:TeleportToPrivateServer(game.PlaceId,code,players)
					end
				end
			end
		end)
local TPService = game:GetService("TeleportService") -- Get the TP service

local Target = 0 -- UserId of the player you want to TP, leave as 0 if you want to TP everyone

function Teleport() -- This function can be called at any time
local TPCode = TPService:ReserveServer(game.GameId) -- This can also be changed to an existing server code
local PlrsToTP = {}

for _, Player in pairs(game:GetService("Players"):GetPlayers()) do
	if Player.UserId == Target or Target == 0 then
		table.insert(PlrsToTP,#PlrsToTP+1,Player) -- Insert player to TP into TP table
	end
end

TPService:TeleportToPrivateServer(game.GameId,TPCode,PlrsToTP) -- Teleport
end

Try out this code, let me know if it works.

1 Like

im trying to teleport to an exisiting reservedserver

This solution will teleport players that are selected to new ReservedServer each, since you have the :ReserveServer() in a for loop. Just remove it from the loop and it should be okay.

1 Like

im not trying to make a new reserved server im trying to teleport an already made existing reserved server that has players in it

Then you can just replace the

local TPCode = TPService:ReserveServer(game.GameId)

in my code with

local TPCode = AlreadyCreatedServerID

1 Like