Teleport Players

Challenge1.Event:Connect(function()
	
	local AllPlayers = #Players:GetPlayers()
	local HalfPlayers = math.round(AllPlayers * 0.5)
	
	local RightSidePos = Vector3.new(RightSide.Position)
	local LeftSidePos = Vector3.new(LeftSide.Position)
	
	RaiseWall1:Fire()
	wait(5)
	
	for i, v in pairs(Players:GetPlayers()) do 
		v.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(RightSidePos)
	end
	
	wait()
	
	for i, v in pairs(HalfPlayers) do -- Error here, It's expecting something else, not a number
		v.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(LeftSidePos)
	end
	
end)

When I call HalfPlayers in the for loop, it always seems to error.

I’m not sure how to combat this.

Any help is appreciated!

I think this is the issue, you are getting the number of Players not the actual Players.

I would create a new variable and do something like this:

local AllPlayers = Players:GetPlayers()
local NumOfPlayers = #AllPlayers
local HalfPlayers = math.round(NumOfPlayers * 0.5)
1 Like

Because HalfPlayers is a number, not a table.

Instead if you want half the players left over loop through them like so, first we get all the players

local PlayersLeft = Players:GetPlayers()

Now we will remove 50%.

-- i stands for how many times you remove players from the array
for i = 1,math.round(#PlayersLeft*.5) do
   -- remove a random value from the table.
   table.remove(PlayersLeft, math.random(#PlayersLeft))
end

Now just loop through the function like so:

for _, player in pairs(PlayersLeft) do
   ...
end
1 Like

This is a bit confusing, how would I incorporate this into my already existing script?

I tried this, it didn’t work. Thank you though!

Just replace

With

PlayersLeft is half the players.

1 Like

Did you get an error of some sort?

1 Like
for _, player in pairs(PlayersLeft) do
	_.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(LeftSidePos)
end

This is what I put and it still doesn’t work, but it doesn’t have any errors either.
(every thing else is the same)

Yes, just the same one I got with just putting:

local AllPlayers = #Players:GetPlayers()
local HalfPlayers = math.round(AllPlayers * 0.5)

Instead of:

_.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(LeftSidePos)

Do:

player.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(LeftSidePos)
1 Like

Can’t you just do this?

local Players = game:GetService("Players"):GetPlayers()
local HalfPlayers = {}

for i = 1,#Players / 2 do
	if #Players == 0 or 1 then return end
	table.insert(HalfPlayers, Players[math.random(#Players)])
end

for i, v in (HalfPlayers) do
	v.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(LeftSidePos)
end
1 Like

Yes! That fixed it! Thank you!

1 Like

Thank you for all the solutions you’ve given me!!

1 Like

I tried that, but I couldn’t get it to work. Thank you anyway!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.