Hello! I’m trying to make an escape room that has an option for Singleplayer or Multiplayer (In the lobby). Once you go into the Singleplayer or Multiplayer, it will teleport you into the main game (the actual escape room). I got the multiplayer to work, but the singleplayer is not working.
This is what I have to teleport the player to a server:
local TS = game:GetService("TeleportService")
local placeId = "5841579510"
local code = TS:ReserveServer(placeId)
script.Parent.Touched:Connect(function(player)
TS:TeleportToPrivateServer(placeId,code,player)
end)
Every time it runs, it gives this error: Unable to cast value to Objects
I’m still getting the same error. Here is what I changed the script to:
local TS = game:GetService("TeleportService")
local placeId = 5841579510
local code = TS:ReserveServer(game.PlaceId)
local debounce = false
script.Parent.Touched:Connect(function(player)
if debounce == false then
debounce = true
TS:TeleportToPrivateServer(placeId,code,player)
end
end)
I also added a debounce because I was getting a lot of errors.
Maybe it’s being touched by something that isn’t a player. I’d do print(player) to figure out if something else is touching it. You should add a check to make sure the thing that touched it is a player.
According to TeleportToPrivateServer API Reference, the third argument needs to be an array of players. In this case, you would simply add curly brackets around player. You should probably also just check that the player is returned in the touched event.
local TS = game:GetService("TeleportService")
local placeId = 5841579510
local code = TS:ReserveServer(game.PlaceId)
local debounce = false
script.Parent.Touched:Connect(function(hit)
if debounce == false then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
TS:TeleportToPrivateServer(placeId,code,{player})
debounce = true
end
end
end)
Ok I made a mistake. There was something wrong with my studio, and it would not let me publish my game, so I tested it and it was still with the last script. I restarted my computer and it works now. Thank you for your help!