How to make a frame randomize it's position upon clicking a button?

Okay so I have a clashing mini game and it’s supposed to randomize the position of the White Bar after a button is clicked;

But it’s not working for some reason and here is the code that I have;

    local WhiteBar = Frame.WhiteBar
	local HitBox = RedArrow.HitBox
	local ActiveArrow = Instance.new('StringValue'); ActiveArrow.Value = 'Active'

	local WhiteBarPosMin, WhiteBarPosMax = WhiteBar:GetAttribute('PositionSizeMin'), WhiteBar:GetAttribute('PositionSizeMax')
	local WhiteBarSizeMin, WhiteBarSizeMax = WhiteBar:GetAttribute('SizeYMin'), WhiteBar:GetAttribute('SizeYMax')
	local RandomPosition = Random.new():NextNumber(WhiteBarPosMin, WhiteBarPosMax)
	local RandomSize = Random.new():NextNumber(WhiteBarPosMin, WhiteBarSizeMax)
	
	WhiteBar.Size = UDim2.new(0.508, 0, RandomSize, 0)
	WhiteBar.Position = UDim2.new(0.5, 0, RandomPosition, 0)
	
	task.spawn(function()
		coroutine.wrap(function()
			while ActiveArrow.Value == 'Active' do
				for i = 0.02, 0.92, 0.01 do
					local Position = UDim2.new(1.348, 0, i, 0)
					RedArrow.Position = Position
					task.wait()
				end

				for i = 0.92, 0.02, -0.01 do
					local Position = UDim2.new(1.348, 0, i, 0)
					RedArrow.Position = Position	
					task.wait()
				end
			end
		end)()
	end)
	
	PressButton.MouseButton1Click:Connect(function()
		WhiteBar.Size = UDim2.new(0.508, 0, RandomSize, 0)
		WhiteBar.Position = UDim2.new(0.5, 0, RandomPosition, 0)
	end)

[Note: This code is inside of a ModuleScript and everything works besides the position randomizer after clicking the button.]

You’re only generating a random value once.

Also, instead of doing:

UDim2.new(0.5, 0, 0.5, 0)

You can do:

UDim2.fromScale(0.5, 0.5)
1 Like

What do you mean I’m only generating a random value once? Also thank you I’ll do that now.

You’re making the variable for the random position once, so everytime you click the button, it’s just using that same variable over and over again.

Would I replace;

UDim2.new(0.5, 0, RandomPosition, 0)

with

UDim2.fromScale(0.5, 0.5)

And should I do the same for;

UDim2.new(0.508, 0, RandomSize, 0)

No, you would replace it with:

UDim2.fromScale(0.5, RandomPosition)

Mine was just an example.

Got you and for it to not use the same one over and over again do I use this;

PressButton.MouseButton1Click:Connect(function()
		local RandomPosition = Random.new():NextNumber(WhiteBarPosMin, WhiteBarPosMax)
		local RandomSize = Random.new():NextNumber(WhiteBarPosMin, WhiteBarSizeMax)
		
		WhiteBar.Size = UDim2.new(0.508, 0, RandomSize, 0)
		WhiteBar.Position = UDim2.new(0.5, 0, RandomPosition, 0)
	end)
1 Like

Yes.

More examples for UDim2.fromScale:

UDim2.new(0.508, 0, RandomSize, 0) would become UDim2.fromScale(0.508, RandomSize)
UDim2.new(0.5, 0, RandomPosition, 0) would become UDim2.fromScale(0.5, RandomPosition)

1 Like

Should I do the same for the WhiteBar.Size line?

1 Like

Thank you, I’ll go ahead and try the updated code and let you know if it worked or not.

1 Like

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