How to make button move to a random spot on hover

as the title said: i want to make a troll game about asking my friend to play roblox, the gui show a title to ask if they want to play, if you hover on “no”, the “no” button automatically move to a random spot

but somehow it just move to my top left corner of the screen, even though the “warn” line work

i tried a lot of solution by using roblox studio built-in assistant, chatgpt, searching up all of the related thing but it wont help

heres the video and the path
Screenshot 2024-02-21 013616

and this is the script

local gui = script.Parent
local yes = gui.yes
local no = gui.no

local function hover()
	no.Position = UDim2.new(math.random(0.1, 0.8), 0, math.random(0.1, 0.8), 0)
	warn("bro tryna click no")
end

no.MouseEnter:Connect(hover)
4 Likes

Try changing the parameters of math.random(). The size a GUI object (to fit the whole screen) is {1, 0} {1, 0}, so you could change those parameters to 0, 1 for each math.random() statement.

So you would have:
no.Position = UDim2.new(math.random(0, 1), 0, math.random(0, 1), 0)
or just:
no.Position = UDim2.fromScale(math.random(0, 1), math.random(0, 1))

1 Like

it doesnt work sadly, i tried the “0, 1” parameter but no, still moving to that corner.

1 Like

try math.random() (with no parameters)

1 Like

math.random goes to full intergers (in your case it will be always 0) so should maybe use Random.New():NextNumber()

local function hover()
	local firstRndm = Random.new():NextNumber(.1, .8)
	local secondRndm = Random.new():NextNumber(.1, .8)
	no.Position = UDim2.new(firstRndm, 0, secondRndm , 0)
	warn("bro tryna click no")
end
2 Likes

You can make your own decimal function, I don’t recommend this tbh because I am pretty sure there is an alternative but it worked so if you’re looking for fast solution:

local no = script.Parent


local function getRandomDecimal()
	local xIndex, yIndex = math.random(1, 9), math.random(1, 9)
	local X, Y
	
	local Decimals = {.1, .2, .3, .4, .5, .6, .7, .8, .9}
	
	local function loop(index)
		for i, v in  Decimals do
			if i == index then
				return v
			end
		end
	end
	X = loop(xIndex)
	Y = loop(yIndex)

	return X, Y
end
local function hover()
	local X, Y = getRandomDecimal()
	
	no.Position = UDim2.new(X, 0, Y, 0)
	warn(no.Position)
	warn("bro tryna click no")
end

no.MouseEnter:Connect(hover)

https://gyazo.com/0ba5f4fa5ec5099954bf6f2735b3b4b5

2 Likes

wowsers! thanks it worked, but i tested the others late reply and theyre worked too! but to be fair i will give the “solution” thing to the earliest reply, sorry for the others :frowning:

1 Like

it worked too but i had to check the solution to the earlier reply, sorry! :sob:

wow it work but sorry i have to give this solution for the earlier reply :frowning:

all good, my code was long anyway that could’ve been replaced with simple math

This is probably the best to use

no.Position = UDim2.new(math.random(1,8)/10,0,math.random(1,8)/10,0)

2 Likes

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