How would I make the text button appear randomly on the screen after it's been clicked?

What I want to happen is that each time the text button is clicked, it appears in a random spot on the screen and you click it again and it just repeats

I tried looking through devforum for something similar and I just found this…

Random Script
>  local r = Random.new()
> 
> local function GetRandomPosition()
> 	return UDim2.new(r:NextNumber(0,1),0,r:NextNumber(0,1),0)
> end

I’m honestly kind of stuck on where I should go from here.
I need the Dice to appear somewhere else on the screen beside that single spot.

I also have this script that is the popup that appear when you click the button.
I wondering if it could be rewritten for the button.

PopUp Script
> click = 0
> local plr = game.Players.LocalPlayer
> while wait() do
> 	if click ~= plr.leaderstats.Points.Value then
> 		local random = math.random(1,1000)
> 		local xnew = random/1000
> 		local new = game.ReplicatedStorage.ShowDice:Clone()
> 		new.Parent = script.Parent.Main
> 		new.Position = UDim2.new(xnew,0,1,0)
> 		new.Text = ""..plr.leaderstats.Points.Value - click.."🎲"
> 		click = plr.leaderstats.Points.Value
> 	end
> end

I tried a click function
but didn’t work

Click Function

function leftClick()

local random = math.random(1,1000)

local xnew = random/1000

local new = game.ReplicatedStorage.TextButton:Clone()

new.Parent = script.Parent.Main

new.Position = UDim2.new(xnew,0,1,0)

end

script.Parent.MouseButton1Click:Connect(leftClick)

So I added this in the TextButton Local Script in StarterGui and it seem to did something.

New Script
script.Parent.Activated:Connect(function()
	game.Workspace.MainEvent.AddPoint:FireServer()
	script.Parent.Parent.Enabled = false
	wait(1)
	script.Parent.Parent.Enabled = true
	
	wait()
	script.Parent:TweenPosition(UDim2.new(script.Parent.Position.X,0, -0.1, 0))
	wait()

	for i = 1,20 do
		local random = math.random(1,1000)
		local xnew = random/1000
		local new = game.ReplicatedStorage.TextButton:Clone()
		new.Parent = script.Parent.Main
		script.Parent.Position = UDim2.new(xnew,0,1,0)
		wait()
	end
end)

Trying out some TweenPosition

3 Likes

What about the click function didn’t work?

3 Likes

image
The “ShowButton” is the click function I added it in StarterGui
Didn’t do nothing at all.
I moved it to
“TextButton”
Didn’t work either Even changed around the new.Parent

1 Like

Thats because the mouse1Click function is connected to the player gui, not a text button. Use
script.Parent.Button.TextButton.Mouse1Click:Connect(leftClick)

1 Like

Tried it and didn’t seem to work. No error appeared.

1 Like

Add a print statement to check whether the function is actually running.

1 Like

I need the Dice to appear somewhere else on the screen beside that single spot.

math.random should work fine.

2 Likes

I currently am using math.radom

math.random is very unefficient

2 Likes

Oooo I Found the Solution!!

local tool = script.Parent

local plr = game.Players.LocalPlayer

local gui = plr.PlayerGui.Button.TextButton

tool.Activated:Connect(function()

gui.Position = UDim2.new(math.random(), 0, math.random(), 0)

end)

Credit to : How would I make a gui appear on a random spot on the screen - #4 by DoveTheEpic

Copied off of that.

2 Likes

How would I make this so it just goes up by itself when a player earns a coin? Like when they earn 10 coins 10 of them appear and go up without having to click them

1 Like

Are you talking about when the player like touches a coin?

2 Likes

Yeah but without the .touched, so like when their leaderstat goes + 1, 1 coin comes up

1 Like

This should help. This is where I got the popup and gui tutorial from

I don’t know much myself but this is what I used.
Hopefully this helps

2 Likes

Thank you so much. I’ll check it out

1 Like