Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Player teleport locations: How to randomize them?

Asked by 7 years ago
Edited 7 years ago

hi, i have this script;

local target1 = CFrame.new(-167, 189.156, 0)
local target2 = CFrame.new(183, 189.156, 0)
local target3 = CFrame.new(8, 189.156, 175)
local target4 = CFrame.new(8, 189.156, -175)

local targets = {target1, target2, target3, target4}
local target = targets[math.random(#targets)]

for i, player in ipairs(game.Players:GetChildren()) do
      if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
      player.Character.HumanoidRootPart.CFrame = target
   end
end

designed to each of the players in the server to a random location. however it teleports everyone to one location, rather than each player to a random location. help?

0
Move line 7 into the for loop. M39a9am3R 3210 — 7y
0
You should mark an answer as correct or state why the answer isn't correct. shadownetwork 233 — 7y

3 answers

Log in to vote
1
Answered by 7 years ago

math.random() returns just one number each time you call it. Therefore, you need to change it every time the loop runs. However, there is another function to make sure that your number is even more random: math.randomseed(). Adding both of these in will look like this:

local target1 = CFrame.new(-167, 189.156, 0)
local target2 = CFrame.new(183, 189.156, 0)
local target3 = CFrame.new(8, 189.156, 175)
local target4 = CFrame.new(8, 189.156, -175)

local targets = {target1, target2, target3, target4}
local target = targets[math.random(#targets)]

for i, player in ipairs(game.Players:GetChildren()) do
   if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
      player.Character.HumanoidRootPart.CFrame = target
   end
   math.randomseed(math.random(1,50))
   target=targets[math.random(#targets)]
end

Good luck!

1
You should only ever use math.randomseed one time as it will affect the outcome of other scripts as well. There's no necessary need for using randomseed either in the instance of live games as they are already randomly seeded in games. M39a9am3R 3210 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

@ChipioIndustries's answer with a few fixes

math.randomseed() should only be called once and math.randomseed(tick()) is a lot more random then math.randomseed(math.random(1,50))

local target = targets[math.random(#targets)] should only be called in the for loop not before it. If it is called before then you would have one extra call


local target1 = CFrame.new(-167, 189.156, 0) local target2 = CFrame.new(183, 189.156, 0) local target3 = CFrame.new(8, 189.156, 175) local target4 = CFrame.new(8, 189.156, -175) local targets = {target1, target2, target3, target4} math.randomseed(tick()) for i, player in ipairs(game.Players:GetChildren()) do local target=targets[math.random(#targets)] if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then player.Character.HumanoidRootPart.CFrame = target end end
Log in to vote
-1
Answered by
Xiousa 156
7 years ago

Actually, ChipioIndustries made a slight error.

target=targets[math.random(#targets)]

That should be

target=targets[math.random(1, #targets)]

1
I'm afraid you're mistaken. Both pieces of code will return the same thing:  http://image.prntscr.com/image/4ba06b676c8b453bb2379ab014c9407e.png Goulstem 8144 — 7y
1
math.random(#targets) will provide the same result as math.random(1,#targets). If a second argument is not provided to the math's random function, the first argument will be taken as its maximum for selection. M39a9am3R 3210 — 7y

Answer this question