Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Unable to cast value to Object error when trying to teleport players in a table?

I want to teleport all players in a table when the NumPlayers value is 1. But I'm not sure how to get all the players in the table and teleport them to the game. Here's my code:

local PlayerAmount = 1 -- How many players have to be in a teleporter for it to teleport
local TeleportService = game:GetService("TeleportService")
local PlayerNum = script.Parent.Players -- How many players are in the teleporter
local debounce = false

PlayersToTP = {}


script.Parent.SurfaceGui.TextLabel.Text = "Players: 0/10"

PlayerNum.Changed:Connect(function()
	script.Parent.SurfaceGui.TextLabel.Text = "Players: "..PlayerNum.Value.."/10"
end)


script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
		debounce = true
		local Humanoid = hit.Parent.HumanoidRootPart
		local char = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(char)
		Humanoid.CFrame = CFrame.new(game.Workspace.TeleportPart.Position + Vector3.new(0,3,0))
		PlayerNum.Value = PlayerNum.Value +1
		table.insert(PlayersToTP,player)
		wait(1.5)
		debounce = false
		if PlayerNum.Value <= PlayerAmount then
			TeleportService:Teleport(4820164617, PlayersToTP)
		end
	elseif debounce == true then
			print("Debounce is true")
	end
end)

The error comes on line 28. I'm not sure how to fix this, if somebody could help me that would be great! Thanks!

2 answers

✓ Accepted answer

Your issue is very simple, you are passing a table instead of passing the Player object, which the function :Teleport() expects as its second argument.

So you just need to change line #28 to iterate through all the players in the table and teleport each player, which I am not sure why you don't just teleport the player right as he touches the part, however I'll do it your way.

It should look something like this:

if PlayerNum.Value >= PlayerAmount then -- this should be bigger or equal to not smaller than
	for _, v in pairs(PlayersToTP) do
		TeleportService:Teleport(4820164617, v) 
	end
end

-- OR
if PlayerNum.Value >= PlayerAmount then
	TeleportService:TeleportPartyAsync(4820164617, PlayersToTP) -- this takes an array of players instead 
end

Also, a little suggestion that is quicker, easier, and more efficient is Character:MoveTo() which you can use to move a player's character to a position and it always puts them on top of whatever is there, so let us say there is another player in that exact same position it puts them on top of them. More on this here

1 comment

0

Thanks for the answer, greatly explained and easy to read. Works great!

TeleportService:Teleport takes a player instance as the second argument, use TeleportPartyAsync instead

TeleportService:TeleportPartyAsync(placeId, PlayersToTP)

This discussion is preserved as it was. New answers, comments, and votes are closed.