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 4 years ago
Edited 4 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


local xPos = math.random(0.021, 0.953) local yPos = math.random(0.146,0.917) local Point = Instance.new("TextButton") Point.Parent = game.StarterGui.ScreenGui Point.Visible = true Point.Size = script.Parent.TextButton1.Size Point.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 4 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.

local xPos = math.random(21, 953) / 1000
local yPos = math.random(146, 917) / 1000

local Point = Instance.new("TextButton")
Point.Parent = game.StarterGui.ScreenGui
Point.Visible = true
Point.Size = script.Parent.TextButton1.Size
Point.Position = UDim2.new(xPos, yPos)

Hope this helped!

Ad

Answer this question