UI scripting Udim2 problem

Hello,

I’m trying to make a UI shake randomly from it’s position on the screen, but I’m not that good at math.random, nor do I know how to get this to work, because UDim2 it’s just annoying to use for me.

local RANDOM_VALUE = math.random(-50, 50) + TARGET_VALUE
	
local goal = {value = frame.Position + UDim2.new(0, RANDOM_VALUE, 0, RANDOM_VALUE)}

and it only goes up and down sideways.

Run it in a loop it will pick random positions on the offset.

To make it smooth use a tween, or runservice or a spring!

1 Like

I am using tweenservice, and in a loop.

You set the x and y to the same number making it not feel random,

Instead create a random for both X and Y then, and use the Random.new library for more precision

Changing the offsets “-50,50” will create a bigger gap to pick from

Something like this?

local X_VALUE = math.random(-50, 50)
	local Y_VALUE = math.random(-50, 50)
	
	local NEW_UDIM2 = UDim2.new(X_VALUE, 0, Y_VALUE, 0)
	
	local goal = {value = frame.Position + NEW_UDIM2}

I’m lost so I don’t really know what too do.

You were using offset before

A udim2 has:
X scale, offset
Y scale, offset

Offset is the amount of pixels
Scale is the screen size 1 is full screen

Use offset again like this:
Im on mobile so sorry for my slow replies&possible mistakes

local X_VALUE = math.random(-50, 50)
local Y_VALUE = math.random(-50, 50)

local NEW_UDIM2 = UDim2.new(0, X_VALUE, 0, Y_VALUE)

local goal = {value = frame.Position + NEW_UDIM2}

That worked, but how would I make it so it stays within a like bounded box, so it’ll shake within a box, and cannot go outside of that box?

your fine.

I was gonna say it in that post but the reason for that is that you add to its CURRENT position,

Rather save its original position and add to that

So above the loop make a variable

local OriginalPosition = whatever.Position

And then change this line to:

local goal = {value = OriginalPosition + NEW_UDIM2}

1 Like

That worked, thanks for showing me the ways.

Glad to have solved it for you! Take care

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.