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
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