Ideas on how to make random guis move at random times and random places

So basically, I want to make a start screen where image labels will fly across the screen, random image labels will fly across the screen to random position, from left to right.

My problem is because of my limited experience with scripting, I can’t seem to get a random gui and make it fly across to a random position

What have I tried?
I tried tables, for loops etc. I don’t if I messed up or if they just don’t work.

Video: https://gyazo.com/c059b266a9475189852c8f66d88647f1
basically only one appears at a time and also they keep following the same pattern

My current script:

local RunService = game:GetService("RunService")
function lerp(a, b, c)
	return a + (b - a) * c
end

function quadBezier(t, p0, p1, p2)
	local l1 = lerp(p0, p1, t)
	local l2 = lerp(p1, p2, t)
	local quad = lerp(l1, l2, t)
	return quad
end

function tweenObject(object, pos1, pos2, pos3)

	for i = 1, 100 do

		local t = i/100

		local XScale = quadBezier(t, pos1.X.Scale, pos2.X.Scale, pos3.X.Scale)
		local YScale = quadBezier(t, pos1.Y.Scale, pos2.Y.Scale, pos3.Y.Scale)

		local XOffset = quadBezier(t, pos1.X.Offset, pos2.X.Offset, pos3.X.Offset)
		local YOffset = quadBezier(t, pos1.Y.Offset, pos2.Y.Offset, pos3.Y.Offset)

		object.Position = UDim2.new(XScale, XOffset, YScale, YOffset)

		RunService.RenderStepped:Wait()

	end
end

while true do
	local template1 = script.Parent.Template1:Clone()
	template1.Parent = script.Parent
	template1.Visible = true
	tweenObject(template1, UDim2.new(0, 0,0.92, 0), UDim2.new(0.496, 0,0.122, 0), UDim2.new(0.951, 0,0.931, 0))
	template1:Destroy()
	local template2 = script.Parent.Template2:Clone()
	template2.Parent = script.Parent
	template2.Visible = true
	tweenObject(template2,UDim2.new(-0.03, 0,0.647, 0),UDim2.new(0.476, 0,-0.035, 0),UDim2.new(0.983, 0,0.485, 0))
	template2:Destroy()
	local template3 = script.Parent.Template3:Clone()
	template3.Parent = script.Parent
	template3.Visible = true
	tweenObject(template3,UDim2.new(0.59, 0,0.952, 0),UDim2.new(0.649, 0,0.573, 0),UDim2.new(0.978, 0,0.634, 0))
	template3:Destroy()
end

yes I know I could keep cloning and changing the cords but I feel like there is better way that I am not aware of.

1 Like

You have limited coding experience but your using BEZIER? (I’ve been coding for over 4 years and I haven’t even touched bezier) Anyways, you can do this in one line by doing

GuiObject:TweenPosition(UDim2.new(ending coordinates here), Enum.EasingDirection.easingdirectionhere, Enum.EasingStyle.easingstylehere, time it takes to move there)

I just found it out yesterday, from I another post I made yesterday.

1 Like

How would I make this fit with bezier tho?

Does bezier output with UDim2?

You give what you are moving and the 3 cords, and it makes a smooth shape insteadBezier2 of a triangle

Tho I cant seem to make more then 1 run at the same time from the same script
@Grayseon

You could put the script where it moves it into a function, and then run it multiple times (the part where you tell it to run should be after you state what the function is .)

alright let me try, be right back

I tried this and its still the same
video: Screen capture - c059b266a9475189852c8f66d88647f1 - Gyazo

local RunService = game:GetService("RunService")
function lerp(a, b, c)
	return a + (b - a) * c
end

function quadBezier(t, p0, p1, p2)
	local l1 = lerp(p0, p1, t)
	local l2 = lerp(p1, p2, t)
	local quad = lerp(l1, l2, t)
	return quad
end

function tweenObject(object, pos1, pos2, pos3)

	for i = 1, 100 do

		local t = i/100

		local XScale = quadBezier(t, pos1.X.Scale, pos2.X.Scale, pos3.X.Scale)
		local YScale = quadBezier(t, pos1.Y.Scale, pos2.Y.Scale, pos3.Y.Scale)

		local XOffset = quadBezier(t, pos1.X.Offset, pos2.X.Offset, pos3.X.Offset)
		local YOffset = quadBezier(t, pos1.Y.Offset, pos2.Y.Offset, pos3.Y.Offset)

		object.Position = UDim2.new(XScale, XOffset, YScale, YOffset)

		RunService.RenderStepped:Wait()

	end
end

function HopefullyThisWork(template,po1,po2,po3)
	local templateClone = template:clone()
	templateClone.Parent = script.Parent
	templateClone.Visible = true
	tweenObject(templateClone,po1,po2,po3)
	templateClone:Destroy()
end

while true do
	local template1 = script.Parent.Template1
	HopefullyThisWork(template1, UDim2.new(0, 0,0.92, 0), UDim2.new(0.496, 0,0.122, 0), UDim2.new(0.951, 0,0.931, 0))
	local template2 = script.Parent.Template2
	HopefullyThisWork(template2,UDim2.new(-0.03, 0,0.647, 0),UDim2.new(0.476, 0,-0.035, 0),UDim2.new(0.983, 0,0.485, 0))
	local template3 = script.Parent.Template3
	HopefullyThisWork(template3,UDim2.new(0.59, 0,0.952, 0),UDim2.new(0.649, 0,0.573, 0),UDim2.new(0.978, 0,0.634, 0))
end

Is the while loop necessary in this script? I don’t understand bezier, but a while loop will stop the rest of your code from running.

I don’t know my self lol, like I said before

1 Like

What you need my friend is the Random Instance, or alternatively math.random. Here is a script that will move your objects across the screen.

-- // Services
local RunService = game:GetService("RunService")

-- // Objects
local Template = ... -- Replace with template object
local ui = ... -- Replace with place you want make it show

-- // Raw
local debounce = false

-- // Functions
function lerp(a, b, c)
	return a + (b - a) * c
end

function quadBezier(t, p0, p1, p2)
	local l1 = lerp(p0, p1, t)
	local l2 = lerp(p1, p2, t)
	local quad = lerp(l1, l2, t)

	return quad
end

function tweenObject(object, pos1, pos2, pos3)
	for i = 1, 100 do
		
		local t = i/100
		
		local XScale = quadBezier(t, pos1.X.Scale, pos2.X.Scale, pos3.X.Scale)
		local YScale = quadBezier(t, pos1.Y.Scale, pos2.Y.Scale, pos3.Y.Scale)

		local XOffset = quadBezier(t, pos1.X.Offset, pos2.X.Offset, pos3.X.Offset)
		local YOffset = quadBezier(t, pos1.Y.Offset, pos2.Y.Offset, pos3.Y.Offset)
		
		object.Position = UDim2.new(XScale, XOffset, YScale, YOffset)
		
		RunService.RenderStepped:Wait()
		
	end
end

function onStep()
	-- // DeBounce
	if (debounce == true) then return end
	debounce = true
	
	-- // Numbers
	local random = Random.new()
	local n1 = random:NextNumber(0, 1) -- pos1 and pos3 Y position
	local n2 = random:NextNumber(0.3, 0.7) -- pos2 X position
	local n3 = random:NextNumber(0, 1) -- pos2 Y position, change these values to make the curve noticeable
	
	local pos1 = UDim2.new(-0.25, 0, n1, 0)
	local pos2 = UDim2.new(n2, 0, n3, 0)
	local pos3 = UDim2.new(1.25, 0, n1, 0)
	
	local color = Color3.new( -- To make a different color, remove if you want
		random:NextNumber(0, 1),
		random:NextNumber(0, 1),
		random:NextNumber(0, 1)
	)
	
	-- // Clone Template
	local new_object = Template:Clone()
	new_object.BackgroundColor3 = color
	new_object.Position = pos1
	new_object.Parent = ui
	
	-- // Call
	tweenObject(new_object, pos1, pos2, pos3)
	
	-- // Discard
	new_object:Destroy()
	
	-- // DeBounce 2 Electric Boogaloo
	debounce = false
end

-- // Connect
RunService.RenderStepped:Connect(onStep)

1 Like

The thing is I don’t want to change the colour I want to change the image of the image label

To do that, simply create a table that will store your image Id’s :

local imageIds = 
{

alright, I will try this, and also

local Template =  script.Parent.Template1
local ui = ?

what should I put in ui?

Simply make a table with image ids:

local imageIds = 
	{
		5365990711; -- Insert Id's here
		265790768;
	}

And after replace onStep with this:

function onStep()
	-- // DeBounce
	if (debounce == true) then return end
	debounce = true
	
	-- // Numbers
	local random = Random.new()
	local n1 = random:NextNumber(0, 1) -- pos1 and pos3 Y position
	local n2 = random:NextNumber(0.3, 0.7) -- pos2 X position
	local n3 = random:NextNumber(0, 1) -- pos2 Y position
	
	local pos1 = UDim2.new(-0.25, 0, n1, 0)
	local pos2 = UDim2.new(n2, 0, n3, 0)
	local pos3 = UDim2.new(1.25, 0, n1, 0)
	
	local assetId = imageIds[math.random(1, #imageIds)]
	
	-- // Clone Template
	local new_object = Template:Clone()
	new_object.Image = "rbxthumb://type=Asset&id=".. assetId .."&w=420&h=420"
	new_object.Position = pos1
	new_object.Parent = ui
	
	-- // Call
	tweenObject(new_object, pos1, pos2, pos3)
	
	-- // Discard
	new_object:Destroy()
	
	-- // DeBounce 2 Electric Boogaloo
	debounce = false
end

Replace the … with the path where you want to put it

local ui = script.Parent
1 Like

Nothing appears on my screen, nothing in the output
script:

-- // Services
local RunService = game:GetService("RunService")

-- // Objects
local Template = script.Parent.Template1
local ui = script.Parent

local imageIds = 
	{
		6583308264; -- Insert Id's here
		2874936636;
		26884682
	}

-- // Raw
local debounce = false

-- // Functions
function lerp(a, b, c)
	return a + (b - a) * c
end

function quadBezier(t, p0, p1, p2)
	local l1 = lerp(p0, p1, t)
	local l2 = lerp(p1, p2, t)
	local quad = lerp(l1, l2, t)

	return quad
end

function tweenObject(object, pos1, pos2, pos3)
	for i = 1, 100 do

		local t = i/100

		local XScale = quadBezier(t, pos1.X.Scale, pos2.X.Scale, pos3.X.Scale)
		local YScale = quadBezier(t, pos1.Y.Scale, pos2.Y.Scale, pos3.Y.Scale)

		local XOffset = quadBezier(t, pos1.X.Offset, pos2.X.Offset, pos3.X.Offset)
		local YOffset = quadBezier(t, pos1.Y.Offset, pos2.Y.Offset, pos3.Y.Offset)

		object.Position = UDim2.new(XScale, XOffset, YScale, YOffset)

		RunService.RenderStepped:Wait()

	end
end

function onStep()
	-- // DeBounce
	if (debounce == true) then return end
	debounce = true

	-- // Numbers
	local random = Random.new()
	local n1 = random:NextNumber(0, 1) -- pos1 and pos3 Y position
	local n2 = random:NextNumber(0.3, 0.7) -- pos2 X position
	local n3 = random:NextNumber(0, 1) -- pos2 Y position

	local pos1 = UDim2.new(-0.25, 0, n1, 0)
	local pos2 = UDim2.new(n2, 0, n3, 0)
	local pos3 = UDim2.new(1.25, 0, n1, 0)

	local assetId = imageIds[math.random(1, #imageIds)]

	-- // Clone Template
	local new_object = Template:Clone()
	new_object.Image = "rbxthumb://type=Asset&id=".. assetId .."&w=420&h=420"
	new_object.Position = pos1
	new_object.Parent = ui

	-- // Call
	tweenObject(new_object, pos1, pos2, pos3)

	-- // Discard
	new_object:Destroy()

	-- // DeBounce 2 Electric Boogaloo
	debounce = false
end

-- // Connect
RunService.RenderStepped:Connect(onStep)

Can I see your ScreeenGui / script hierarchy?

:stuck_out_tongue: I can say that I was pretty much in the same boat, although bezier isn’t really that difficult since it’s just an application of a calculus formula. (eg: use the internet to derive the formula, or learn to derive it yourself)

this looks really complex right, but it’s not that bad

Here’s an application with 5 points

There’s a clear pattern which you can figure out.

(Here’s some code I wrote not too long ago to show this)

local Workspace = game:GetService("Workspace");

local P0 = Workspace.Positions.P0;
local P1 = Workspace.Positions.P1;
local P2 = Workspace.Positions.P2;

local function B(t)
	return ((1-t)^2)*P0.Position + 2*t*((1-t))*P1.Position + t*t*P2.Position
end

Anyways to get back to the OP’s question

To fix that we can probably use spawn() or coroutine.wrap(), what this does is create a new thread (meaning you can run multiple lines of code simultaneously)

-- something like this
while true do
	spawn(function()
		local template1 = script.Parent.Template1:Clone()
		template1.Parent = script.Parent
		template1.Visible = true
		tweenObject(template1, UDim2.new(0, 0,0.92, 0), UDim2.new(0.496, 0,0.122, 0), UDim2.new(0.951, 0,0.931, 0))
		template1:Destroy()
	end)
	spawn(function()
		local template2 = script.Parent.Template2:Clone()
		template2.Parent = script.Parent
		template2.Visible = true
		tweenObject(template2,UDim2.new(-0.03, 0,0.647, 0),UDim2.new(0.476, 0,-0.035, 0),UDim2.new(0.983, 0,0.485, 0))
		template2:Destroy()
	end)
	spawn(function()
		local template3 = script.Parent.Template3:Clone()
		template3.Parent = script.Parent
		template3.Visible = true
		tweenObject(template3,UDim2.new(0.59, 0,0.952, 0),UDim2.new(0.649, 0,0.573, 0),UDim2.new(0.978, 0,0.634, 0))
		template3:Destroy()
	end)
    wait(5)
end

Regarding them moving to the same position, it’s probably because your end positions are really similar.
I would say maybe instead of having them all spawn like on the left side, maybe make them spawn right and move left

	tweenObject(template1, UDim2.new(1, 0,0.92, 0), UDim2.new(0.496, 0,0.122, 0), UDim2.new(0, 0,0.931, 0)) -- moves right to left
1 Like

learnt something new today, Thanks a lot.