Making a gui move when clicked

I was trying to make a script so that when a textbutton is clicked, the gui would move. It doesn’t move when I click it for some reason, and there aren’t any errors. Here is the script:

local xPos = math.random(.01, .95)
local yPos = math.random(.01, .95)

while wait(.05) do
	xPos = math.random(.01, .95)
	yPos = math.random(.01, .95)
end

script.Parent.MouseButton1Click:Connect(function()
script.Parent.Position = UDim2.fromScale(xPos, yPos)
end)

I also tried using script.Parent.Position = UDim2.new(xPos, 0, 0, yPos) , and still nothing.

Script type: LocalScript
Parent: TextButton
Parent of TextButton: Frame
Parent of Frame: ScreenGUI
Parent of ScreenGUI: StarterGUI

If you have any questions, feel free to ask! Thanks!

3 Likes

You have to tween position it. So detect when the text button is clicked then tween position the text button to the coordinates you want and you can also add effects to it as well like a bouncy effect. With your case you could do this.

script.Parent.MouseButton1Click:Connect(function()
       script.Parent:TweenPosition(UDim2.new(XScale, XOffset, YScale, YOffset), Enum.EasingDirection, Enum.EasingStyle, howLongYouWantItToTakeGoesHere)
end)

Check this API reference for more information: GuiObject | Documentation - Roblox Creator Hub

3 Likes

Also, if it solved your problem let me know. Maybe check my comment as a soultion :smirk: I’m playing anyways let me know though.

Ok, I replaced the

script.Parent.MouseButton1Click:Connect(function()
script.Parent.Position = UDim2.fromScale(xPos, yPos)
end)

with this

script.Parent.MouseButton1Click:Connect(function()
	script.Parent:TweenPosition(UDim2.new(xPos, 0, yPos, 0), Enum.EasingDirection, Enum.EasingStyle, 0)
end)

and for some reason it still doesn’t work
no errors

It’s because you have to put your own Enum.EasingDirection and Enum.EasingStyle after you write those put another . and choose which one. It should automatically pop up

1 Like

Also, the parameters for the UDim2.new have to be the position you want the text button to end up in.

Try removing the while loop and put its contents in the click function Instead

2 Likes

Okay, I did it, but it didn’t work. Did I use a wrong combination of easing styles and directions?

script.Parent.MouseButton1Click:Connect(function()
	script.Parent:TweenPosition(UDim2.new(xPos, 0, yPos, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0)
end)

Oh, just now saw that post! I’ll try it!

1 Like

Well, I tried it. It moved. But it moved to the top left corner and when i clicked it again it wouldn’t move anymore.

local xPos = math.random(.01, .95)
local yPos = math.random(.01, .95)

script.Parent.MouseButton1Click:Connect(function()
	script.Parent:TweenPosition(UDim2.new(xPos, 0, yPos, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0)
	xPos = math.random(.01, .95)
	yPos = math.random(.01, .95)
end)

Because of the EasingDirection and EasingStyle you can just remove those.

1 Like

Like this?

script.Parent.MouseButton1Click:Connect(function()
	script.Parent:TweenPosition(UDim2.new(xPos, 0, yPos, 0))
	xPos = math.random(.01, .95)
	yPos = math.random(.01, .95)
end)

Ah, the reason is because they way how roblox does math.random

You cannot have decimal values in your parameters. They values have to be whole numbers.

1 Like

For the math.random just put 0 and 1 as the parameters and check how it looks.

Wait, so then how would I get a random decimal, because if I want scale sizes, I have to use decimals, right?

Just use 0 and 1 as the parameters.

1 Like

That won’t work either. Roblox only allows whole number intervals in math.random. if you need decimals in your output, you can do something like this

value = math.random(1, 100) / 100

1 Like

No it works but the returning value will either be 0 or 1 so yeah you’re right.

1 Like

You can have decimal values in your parameters, such as in scale

@ForegoingTyler12943
Try this:


script.Parent.MouseButton1Click:Connect(function()
    local xPos = math.random(1, 95)/100
    local yPos = math.random(1, 95)/100
    script.Parent:TweenPosition(UDim2.new(xPos, 0, yPos, 0))
end)

You should make the random values whole numbers and divide by 100

4 Likes

That’s exactly what I was talking about.