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?
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!
@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
Actually, ChipioIndustries made a slight error.
target=targets[math.random(#targets)]
That should be
target=targets[math.random(1, #targets)]