"Unable to cast value to object" when I use :ReserveServer

  1. What do you want to achieve?
    I want to be able to teleport a table of players to a place.
  2. What is the issue?
    “Unable to cast value to object” gets printed when I pcalled a function to teleport a table of players.
  3. What solutions have you tried so far?
    Tried TeleportPartyAsync, TeleportAsync, etc. ReserveServer is causing this issue tho.

Code:

--2

local tpPart = script.Parent.TpPart
local PlayersIn = {}
local event = workspace.OnExit
local gui = script.Parent.ExitGui
local timerLabel = script.Parent.Timer.SurfaceGui.TimerLabel
local tps = game:GetService("TeleportService")
local sound = script.Parent.Timer.Sound
local placeId = 10947625152
local pa = tostring(script.Parent.PlayersAllowed.Value)
local db = false

local function Teleport()
	local succ,err = pcall(function() 
		if #PlayersIn > 0 then
			local Code = tps:ReserveServer(placeId)
			for i = 1, #PlayersIn do
				if game.Players:FindFirstChild(PlayersIn[i]) then
					tps:TeleportToPrivateServer(placeId, Code, game.Players:FindFirstChild(PlayersIn[i]))
				else
					table.remove(PlayersIn, i)
				end
			end
		end
	end)
	if err then
		warn(err)
	end

end


script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if #PlayersIn <= script.Parent.PlayersAllowed.Value then
			if db ~= true then
				db = true
				local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
				if table.find(PlayersIn, plr.Name) then
					return
				else
					gui:Clone().Parent = plr.PlayerGui
					table.insert(PlayersIn, plr.Name)
					script.Parent.BillboardGui.Players.Text = #PlayersIn.." / "..pa
					local char = plr.Character
					local hrp = char.HumanoidRootPart
					hrp.CFrame = CFrame.new(tpPart.CFrame.p) + Vector3.new(0,0.1,0)
				end
				wait(1)
				db = false
			end
		end
	end
end)

event.OnServerEvent:Connect(function(plr)
	if table.find(PlayersIn, plr.Name) then
		for i = 1, #PlayersIn do
			if PlayersIn[i] == plr.Name then
				table.remove(PlayersIn, i)
			end
		end
		script.Parent.BillboardGui.Players.Text = #PlayersIn.." / "..pa
	end
end)

while wait() do
	Timer = 20

	for i = 1, Timer do
		Timer = Timer - 1
		timerLabel.Text = "S:0"..Timer
		wait(1)
	end
	sound:Play()
	sound.Ended:Wait()

	Teleport()
end
1 Like

It doesn’t actually look like PlayersIn is set to anything anywhere, so PlayersIn[i] is probably nil

1 Like

Organized code to really find the error and updated

--2

local tpPart = script.Parent.TpPart
local PlayersIn = {}
local event = workspace.OnExit
local gui = script.Parent.ExitGui
local timerLabel = script.Parent.Timer.SurfaceGui.TimerLabel
local tps = game:GetService("TeleportService")
local sound = script.Parent.Timer.Sound
local placeId = 10947625152
local pa = tostring(script.Parent.PlayersAllowed.Value)
local db = false

local function Teleport()
	if #PlayersIn > 0 then	return	end
	
	local succ, Code = pcall(tps.ReserveServer, tps, placeId)
	if not succ then	return warn(Code)	end
	
	local Success, Error = pcall(tps.TeleportToPrivateServer, tps, placeId, Code, PlayersIn)
	if not Success then	return warn(Error)	end
end


script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if #PlayersIn <= script.Parent.PlayersAllowed.Value then
			if not db then
				db = true
				local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
				if not table.find(PlayersIn, plr) then
					gui:Clone().Parent = plr.PlayerGui
					table.insert(PlayersIn, plr)
					script.Parent.BillboardGui.Players.Text = #PlayersIn.." / "..pa
					local char = plr.Character
					local hrp = char.HumanoidRootPart
					hrp.CFrame = CFrame.new(tpPart.CFrame.p) + Vector3.new(0,0.1,0)
				end
				task.wait(1)
				db = false
			end
		end
	end
end)

event.OnServerEvent:Connect(function(plr)
	local Index = table.find(PlayersIn, plr)
	if Index then
		table.remove(PlayersIn, Index)
		script.Parent.BillboardGui.Players.Text = #PlayersIn.." / "..pa
	end
end)

while task.wait() do
	Timer = 20

	for i = 1, Timer do
		Timer = Timer - 1
		timerLabel.Text = "S:0"..Timer
		task.wait(1)
	end
	sound:Play()
	sound.Ended:Wait()

	Teleport()
end

Source of the updated: task

1 Like