How to get a UI to spawn randomly

Currently I am developing a game and thinking about having a UI that pops up randomly on a players screen in many different places. If anyone has suggestions to how this would work that would be great! I’ll take all the help I can get.

5 Likes

What have you tried so far to implement this, and where are you stuck?

So basically I’m trying to have it so when I click a button I have a UI popping up that you clicked it and then it goes away. But for monetization reasons I would like to have it so each time they click the button the UI pops up in different places on their screen.
I have tried cloning it from replicated storage and having different positions using math.random but it does not seem to be efficient enough.

What do you mean by that? Do you get spikes of low FPS whenever a clone is added?

No just it only runs the not visible once.

What kind of hi and what it says

So, what? Clicking the first clone that pops up works fine, but the following ones don’t close when you press the button?

Please post your code if you want help with it.

 local Players = game.Players.LocalPlayer
local Clicks = Players.leaderstats.Clicks
local PlayerGui = Players.PlayerGui
local ClickGui = PlayerGui.Guis.ICONS.ClickNumber
local ClickPop = game.ReplicatedStorage.ClickText

script.Parent.MouseButton1Click:Connect(function()
	Clicks.Value = Clicks.Value + 1
	if Clicks.Value >= 1 then
		ClickGui.Text = Clicks.Value
			end
end)

The spacing didn’t register sorry about that.

In that code I deleted what I tried before since it did not work. So I just had the code I started with. But the code pretty much shows that each time the player clicks the button it changes the leaderstats and the thing that displays the number. And the ClickPop is the thing that is supposed to be cloned.

You can’t actually change the values in a Player’s leaderstats from a LocalScript. You’ll need to fire a RemoteEvent that a server/normal script listens to and change the leaderstats value from that server script.

Have you tried something like this to make the GUI go away when you click it?

script.Parent.MouseButton1Click:Connect(function()
	script.Parent:Destroy()
end)

No… That has nothing to do with what I need for this. I just need the ClickPop to spawn randomly everything else works.

local Players = game.Players.LocalPlayer
local Clicks = Players.leaderstats.Clicks
local PlayerGui = Players.PlayerGui
local ClickGui = PlayerGui.Guis.ICONS.ClickNumber
local pos1 = UDim2.new(0.591,0,0.367,0)
local pos2 = UDim2.new(0.265,0,0.314,0)
local ClickPop = game.ReplicatedStorage.ClickText


script.Parent.MouseButton1Click:Connect(function()
	Clicks.Value = Clicks.Value + 1
	if Clicks.Value >= 1 then
		ClickGui.Text = Clicks.Value
		ClickPop:Clone().Parent = PlayerGui.Guis
		ClickPop.Name = "Click"
		PlayerGui.Guis.Click:TweenPosition(math.random(pos1,pos2))
			end
end)

randomly, u mean that it’s not necessary to be destroyed

That is what I tried before with the ClickPop going on the UI randomly as a test but it is not efficient enough.

i saw that you use TweenPosition. Why not changing PlayerGui.Guis.Click.Position

that was one , two you didn’t even call the tween service

Something like this, then?

script.Parent.MouseButton1Click:Connect(function()
	Clicks.Value = Clicks.Value + 1
	if Clicks.Value >= 1 then
		ClickGui.Text = Clicks.Value
		ClickPop:Clone().Parent = PlayerGui.Guis
		ClickPop.Name = "Click"
		
		local newPos = UDim2.new(0math.random(), 0, math.random(), 0)
		PlayerGui.Guis.Click:TweenPosition(math.random(newPos))
	end
end)

I could try that thanks. Then I could do wait(2) then make it not visible correct?