Math.random or CFrame not working correctly

Hey! I’m making a script which teleports 1 of 3 dummies to 1 of 3 randomly selected parts. Yet it doesn’t seem to work.
Script:

local children = {game.Workspace.Players:GetChildren()}
local sp = {game.Workspace.Part1, game.Workspace.Part2, game.Workspace.Part3}

wait(1)

local num = math.random(#sp)
local randomplace = sp[num]
	
local randomplayernum = math.random(#children)
print(randomplayernum)
local player1 = children[randomplayernum]
	
	
	
print(randomplace)
	
	
local name = player1.Name
	
wait(5)
	
local theplayer = children[name]
if theplayer then
	children[randomplayernum].HumanoidRootPart.CFrame = sp[num].CFrame
		
	table.remove(sp, num)
	table.remove(children, randomplayernum)
end

1 Like

Here is a possible solution:

  1. Make a folder in workspace with the parts that will be chosen from.

  2. Make a variable that gets the parts from the folder.

local parts = workspace.Folder:GetChildren()
  1. Now grab a random instance out of it.
local randompart = parts[math.random(1, #parts)]
  1. Now do the same thing for the player.
local players = game.Workspace.Players:GetChildren()
local randomplayer = players[math.random(1, #players)]
  1. Now you should be able to teleport the randomly chosen player to the part.
randomplayer.PrimaryPart.CFrame = randompart.CFrame
1 Like

I’ll try it in a bit, if it works, i’ll mark the comment as solution

1 Like

Now how do I remove parts that can’t be chosen anymore?

1 Like

I am assuming that after the dummy gets teleported to a part, you want that part to be unable to be teleported to.

To do this, first you need to delete, or change the parent of the previous part.

randompart.Parent = workspace -- Change the parent of the part
randompart:Destroy() -- Deletes the part

Then, select a new random part.

local randompart = parts[math.random(1, #parts)]

Now you can teleport to that part.

randomplayer.PrimaryPart.CFrame = randompart.CFrame
1 Like

Yes it’s for a soccer game, but don’t destroy the parts. I have to use them every round.

1 Like

So I need it in a sort of table if that is possible

1 Like

Then make a folder in workspace called something like “teleportedparts”

Then once you’ve teleported to the part it would put it in that folder

randompart.Parent = workspace.teleportedparts

Then when the round ends, just put all the parts back into the previous folder.

for i,v in pairs(workspace.teleportedparts:GetChildren()) do
v.Parent = workspace.Folder -- The folder the parts were previously in
end

That should work.

1 Like

I understand now, so when a new round starts I’ll teleport the children of ‘teleportedparts’ to the normal folder

1 Like

Also, I want the players to stop teleporting. 1 spawnpoint for each player.

1 Like

I’m using dummies right now, but I mean actual player characters

1 Like

It should be

local num = math.random(1,#sp)

The same issue can be found here

When again it should be :

local randomplayernum = math.random(1,#children)
1 Like

It should use 1 spawnpoint for each player.

1 Like

end

Workspace:
Part1
Part2
Part3
Players
Player1
Player2
Player3
Player4
Player5
Player6

The thing is, it works sometimes, but sometimes it doesn’t work and I don’t know why.
Thanks in advance!

Answering your question:
local children = {game.Workspace.Players:GetChildren()}

This creates an array with 1 item, which is the array of players.
local num = math.random(#sp)

This gives a random number. But since sp is an array of 3 items, this will give you a number from 1 to 3.
local randomplace = sp[num]

This gives you a part from sp with a random index.
local randomplayernum = math.random(#children)

This gives you a random number from 1 to 1, since children contains 1 item.
local player1 = children[randomplayernum]

This gives you the 1st player, but since children contains 1 item, this will be the array of players.
print(randomplayernum)

This will print 1.
print(randomplace)

This will print a part.
local name = player1.Name

This gives the Name property of the array of players to name.
wait(5)

This will wait 5 seconds.
local theplayer = children[name]

This will give theplayer the value of children[name], which is nil since name is the name of the array of players, thus, it is not a valid key for children.
if theplayer then