Unable to cast value to Object

My code:

local players = {}

local teleporting = false

local tpService = game:GetService("TeleportService")

local TweenService = game:GetService("TweenService")
local info = TweenInfo.new(1.5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false)

local close = TweenService:Create(script.Parent.Parent.door, info, {Size = script.Parent.Parent.final.Size, Position = script.Parent.Parent.final.Position})
local open = TweenService:Create(script.Parent.Parent.door, info, {Size = script.Parent.Parent.start.Size, Position = script.Parent.Parent.start.Position})

local max = 8

local function countdown()
	for i=30, 0, -1 do
		script.Parent.Parent.TimeLeft.Value = i
		script.Parent.Parent.display.BillboardGui.time.Text = i
		task.wait(1)
	end
end

script.Parent.Parent.TimeLeft.Changed:Connect(function()
	if script.Parent.Parent.TimeLeft.Value == 0 then
		teleporting = true
		close:Play()
		
		local code = tpService:ReserveServer(11106583075)

		task.wait(2)

		tpService:TeleportToPrivateServer(11106583075, code, players)
		
		for k in pairs (players) do
			players[k] = nil
		end
		
		task.wait(5)
		open:Play()
		teleporting = false
		script.Parent.Parent.TimeLeft.Value = 30
		countdown()
	end
end)

The error:

image

Can you point out which line the error is on?

the players table is empty so it attempts to teleport no one

Yeah this is correct, to fix it just do

game.Players:GetPlayers()
2 Likes

well its not really supposed to teleport all the players and besides a player gets added into the table when he touches a certain part

its at line 33 just look at the image

Then do this,

table.insert(players, "chr variable from touch here")

wait let me show you the second part of the script

thats the second part:

script.Parent.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	
	local inTable = table.find(players,hit.Parent.Name)
	
	local plr = game.Players:FindFirstChild(hit.Parent.Name)
	
	if inTable then return end
	
	if #players >= max then return end
	
	if hum and teleporting == false then
		table.insert(players,hit.Parent.Name)
		print(#players)
		script.Parent.Parent.display.BillboardGui.TextLabel.Text = (#players).. "/" ..max
		
		hit.Parent.HumanoidRootPart.CFrame = script.Parent.Parent.tp.CFrame
		
		plr.PlayerGui.ElevatorGui.Enabled = true
		end
end)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
	table.remove(players,players[player.Name])
	script.Parent.Parent.display.BillboardGui.TextLabel.Text = (#players).. "/" ..max
	
	local char = player.Character
	
	char.HumanoidRootPart.CFrame = script.Parent.Parent.tpleave.CFrame
	
	player.PlayerGui.ElevatorGui.Enabled = false
end)

countdown()

Your line 33 is a blank line. You aren’t teleporting a array of players, it’s an array of strings.

what is this then?

local code = tpService:ReserveServer(11106583075)

task.wait(2)

tpService:TeleportToPrivateServer(11106583075, code, players)

can you do a for i,v in pairs to teleport them individually instead of together.

-- inserting string
table.insert(players,hit.Parent.Name)

-- how to insert player object
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
    table.insert(players, player)
end
1 Like

doesnt work it has to be a table i think

oh wait let me test that thank you

That’s why, “players” is a table and the for i,v will go through the array and teleport them .

its working thank you i marked this post as solved