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

Why does this teleport script only teleport 2 people instead of all players?

When I run the game with 3 players 2 people get tp'd and not everyone. Here is the code.

for i, v in ipairs (game.Players:GetPlayers()) do
local chance = math.random(1,4)
print("starting teleport")

if chance == 1 then
v.Character.HumanoidRootPart.CFrame = CFrame.new(Spawn.Position)


elseif chance == 2 then
v.Character.HumanoidRootPart.CFrame = CFrame.new(Spawn1.Position)


elseif chance == 3 then
v.Character.HumanoidRootPart.CFrame = CFrame.new(Spawn2.Position)


elseif chance == 4 then
v.Character.HumanoidRootPart.CFrame = CFrame.new(Spawn3.Position)
end

The spawn and ghost spawn are just variables for the spawns around the map.

It is supposed to teleport the players in game to spawn 1 2 3 or 4 at random and the randomly chosen player (the ghost) gets teleported to the ghost spawn.

If you can solve this please do.

5 comments

0

You forgot the end of the for...

0

Are you trying to teleport every player or only the 4 at a time?

0

every player (also i have the end i just didnt copy and paste it) i am only having it like that for now but i am going to have 12 players so i will have 11 elseif statements

0

Also ABK im not asking about the math.random im asking why its only teleporting 1 player (other than the ghost) instead of all players

0

I edited it to make more sense of what im trying to fix

1 answer

This may help a little as far as the math portion, it was too big for comments...

--Every time a script in a place uses the math.random function, the random generator generates a random number. However, the random number generator is deterministic. It uses the last random number to generate the next random number. When the scripts starts, the “first” random number is always the same. So every time the script is run, the same random number sequence will be generated.

--When doing something like math.random(1, 100), all the numbers will turn out as whole numbers, which in some cases, just isn’t enough. However, if you did this, then the numbers would be random non-integers between 1 and 100, including decimals.

If you use...

math.randomseed(tick())
for _ = 1, 10 do
	print(math.random(100))
	wait(1)
end

The random numbers should be different every time it runs.

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