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

How do I use instance.new for GUI and make it spawn random?

Asked by 5 years ago
Edited 5 years ago

I am making a Textbutton so it spawns atimaticaly and picks a random spot in the screen GUI. What do I do to make it go to a random position from

x = 0.021 - 0.953 y = 0.146 - 0.917 please help

This is my current code

1local xPos = math.random(0.021, 0.953)
2local yPos = math.random(0.146,0.917)
3 
4local Point = Instance.new("TextButton")
5Point.Parent = game.StarterGui.ScreenGui
6Point.Visible = true
7Point.Size = script.Parent.TextButton1.Size
8Point.Position = UDim2.new(xPos, yPos)

It is a local script but the math.random position is not working

1 answer

Log in to vote
0
Answered by 5 years ago

math.random() only gets whole values. So it can't get any values with decimals. Which means that both your math.random() will return 0. Instead I recommend getting the whole values and then dividing them with 1000 so you get the value you actually intended to get.

1local xPos = math.random(21, 953) / 1000
2local yPos = math.random(146, 917) / 1000
3 
4local Point = Instance.new("TextButton")
5Point.Parent = game.StarterGui.ScreenGui
6Point.Visible = true
7Point.Size = script.Parent.TextButton1.Size
8Point.Position = UDim2.new(xPos, yPos)

Hope this helped!

Ad

Answer this question