Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make this frame move all randomly inside another invisible frame?

script.Parent.Position = math.random(script.Parent.Parent.Position)

This Script inside the Frame that is supposed to move around, should make this frame move around in another frame all random like, because i want to make the worlds most annoying game.

Error: Line 1: invalid argument #1 to 'random' (number expected, got UDim2)

1 comment

0

It even says it in the error (expected, got UDim2)

3 answers

✓ Accepted answer

math.random (or Random objects) can't generate random vectors or cframes or udim2s or whatevers, only numbers

you have to construct a UDim2 manually if you want to give it a random position

local rando = Random.new() -- random objects are Awesomer and cooler than math.random because you don't have to do stupid dumb multiplication stuff to get non-integers
local size = script.Parent.Size
script.Parent.Position = UDim2.new(rando:NextNumber(size.X.Scale/2, 1 - size.X.Scale/2), 0, rando:NextNumber(size.Y.Scale/2, 1 - size.Y.Scale/2), 0)

this is assuming that the frame's AnchorPoint is at 0.5, 0.5 and that it only uses Scale for sizing btw

5 comments

0

ok, that works, but how do i get it to move alot of more times?

0

if you want it to move infinitely, you can use a while true do loop

0

make sure to add a wait though

0

ok

0

damn i took too long lol

What's up Jakob lol, I see you on here a lot. Anyway, the reason your script didn't work is because gui.object position requires 4 numbers. It goes like

guiobject.position = UDim2.new(x_screenscale, x_pixels, y_screenscale, y_pixels)

That's what they mean by UDim2. The error you got is because math.random works with 2 numbers. Inside the parentheses after math.random is the maximum and minimum values. So that's like

math.random(minvalue, maxvalue) 

Anyway, your code should work if you do math.random on the x and y screen scale values. I don't know the correct term for those values if you haven't guess already. Anyway, it should look something like this.

local x = math.random(0,100)
x = (x*0.01)
local y = math.random(0,100)
y = (y*0.01)

script.Parent.Position = UDim.new(x, 0, y, 0)

I'm not 100% sure that this is going to work but the reason I made the values variables is so I can use math.random with large values because I don't think math.random can do decimals. If it can, then I don't know how to do that lol. Anyway, after you do math.random to get your number, you would then multiply it by 0.01 so that the final value is in between 0 and 1. 0 being the left side of the screen or the top and 1 being the right side of the screen or the bottom. If you find that the frame or whatever goes outside of the screen, just change the random min and max values to your liking. Let me know if you have any questions (can't guarantee that I can answer them though lol).

1 comment

0

elyzzia win

local rando = Random.new() -- random objects are Awesomer and cooler than math.random because you don't have to do stupid dumb multiplication stuff to get non-integers
local size = script.Parent.Size

while script.Parent.Visible == true do
	wait(0.01)
	script.Parent.Position = UDim2.new(rando:NextNumber(size.X.Scale/2, 1 - size.X.Scale/2), 0, rando:NextNumber(size.Y.Scale/2, 1 - size.Y.Scale/2), 0)
end

AH YES PERFECTION

1 comment

0

well yes but actually no about elyzzia winning

This discussion is preserved as it was. New answers, comments, and votes are closed.