script.Parent.MouseButton1Click:Connect(function() x = game.Players.LocalPlayer:WaitForChild("PlayerGui").SurfaceGui2.TextButton.Text local RandomNumber = math.random(1,3) for i = 1, (tonumber(x)) do local F9 = Instance.new("Part") F9.Parent = game.Workspace if RandomNumber == 1 then F9.CFrame = CFrame.new(-482.5, 57, -2.5) elseif RandomNumber == 2 then F9.CFrame = CFrame.new(-523.2, 57, -95.1) elseif RandomNumber == 3 then F9.CFrame = CFrame.new(-579, 1.5, -90.8) end end end)
Right now all 9 parts that I want to be spawned are being spawned all together in one of the three locations. Is there anyway I could spawn each of these parts in one of the three locations without having to spawn all 9 in the same location? All help is appreciated!
The problem you're having is that you don't refresh the RandomNumber value, it is before the loop. It should be inside of the loop like this:
script.Parent.MouseButton1Click:Connect(function() x = game.Players.LocalPlayer:WaitForChild("PlayerGui").SurfaceGui2.TextButton.Text --old position for i = 1, (tonumber(x)) do local RandomNumber = math.random(1,3) --new position local F9 = Instance.new("Part") F9.Parent = game.Workspace if RandomNumber == 1 then F9.CFrame = CFrame.new(-482.5, 57, -2.5) elseif RandomNumber == 2 then F9.CFrame = CFrame.new(-523.2, 57, -95.1) elseif RandomNumber == 3 then F9.CFrame = CFrame.new(-579, 1.5, -90.8) end end end)