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

Any idea how to teleport some players in the game to another place instead of all?

I've been making a game with a queue system which means when players touch it, they get tagged. When the part is clicked, everybody that is tagged will be teleported to another place except there's a problem. I keep getting either an "Unable to cast value to objects" error. Here's my code:

Teleport Script (Server Script):

script.Parent.ClickDetector.MouseClick:Connect(function()
if script.Parent.Parent.PlayerCount.Value >= 26 then
script.Parent.Touch.Disabled = true
script.Parent.Parent.NameGui.TextLabel.Text = "Game is about to start..."
wait(3)
script.Parent.Parent.NameGui.TextLabel.Text = "Teleporting players..."
local tele = game:GetService("TeleportService")
local pv = game.Players:GetChildren()
local Server = tele:ReserveServer(4625879335)
for i = 1, #pv do
local value = pv[i]
if value.Teams.Value == true then
tele:TeleportToPrivateServer(4625879335,Server,value)
end
end
end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	if script.Parent.Parent.PlayersJoined:FindFirstChild(plr.Name) ~= nil then
	script.Parent.Parent.PlayerCount.Value = script.Parent.Parent.PlayerCount.Value - 1
	end
end)

and here's the code for the tag script:

local On = true
local function debounce(func)
local isRunning = false
return function(...)
if not isRunning then
isRunning = true
func(...)
isRunning = false
end
end
end

script.Parent.Touched:Connect(debounce(function(part)
    local Humanoid = part.Parent:FindFirstChild("Humanoid")
    if Humanoid then
    local player = game.Players:GetPlayerFromCharacter(part.Parent)
player.Teams.Value = true
    end
    local Play = game.Players:GetPlayerFromCharacter(part.Parent)
	local Plr = Play.UserId
	if script.Parent.Parent.PlayerCount.Value <= 52 and On == true then
	if script.Parent.Parent.PlayersJoined:FindFirstChild(part.Parent.Name) == nil and workspace.SoloQueue.PlayersJoined:FindFirstChild(part.Parent.Name) == nil then
	tag = Instance.new("StringValue")
	tag.Parent = script.Parent.Parent.PlayersJoined
	tag.Name = part.Parent.name
	Play.Teams.Value = true
	script.Parent.Parent.PlayerCount.Value = script.Parent.Parent.PlayerCount.Value + 1
	end
	end
end))

script.Parent.Parent.PlayerCount.Changed:Connect(function()
	script.Parent.Parent.NameGui.TextLabel.Text = "Join Teams queue ("..script.Parent.Parent.PlayerCount.Value.."/52, 26 minimum, click the button to teleport players)"
end)

In line 13 on the teleport script they displayed an error saying "Unable to cast value to objects". Any suggestions?

1 answer

✓ Accepted answer

Server script line 13, third argument should be a table of players not the player object directly.

Here's what's the script is thinking:

local tele = game:GetService("TeleportService")
local pv = game.Players:GetChildren()
local Server = tele:ReserveServer(4625879335)
for i = 1, #pv do
	local value = pv[i] -- getting the player
	if value.Teams.Value == true then
		tele:TeleportToPrivateServer(4625879335,Server,value) -- script errors because of the third argument
	end
end

Here's a better version:

local tele = game:GetService("TeleportService")
local pv = game.Players:GetChildren()
local Server = tele:ReserveServer(4625879335)
local Table_1={};
for i,v in pairs(pv)do -- i = index, v = table[i] or the value
	if v.Teams.Value == true then -- if statement filters some players
		table.insert(Table_1,v); --insert a player into this table
	end;
end;
tele:TeleportToPrivateServer(4625879335,Server,Table_1) -- it will tp the players from Table_1

Source -> https://developer.roblox.com/en-us/api-reference/class/TeleportService Please open output from the view tab to find errors (red text), some are clickable to show you the location.

1 comment

0

well you learn something new everyday. I didn't even know how tables were properly used in script. Thanks for the help.

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