Destroying gui not working

I have this script and I want it to destroy the gui its in that would be script.Parent.Parent but everytime I put destroy it destroys before the script is run?

local fr = script.Parent
local clone = script.Frame
local imageClone = script.Logo
local Main = script.Parent.Parent
local RunService = game:GetService("RunService")
local connection
local ContentProvider = game:GetService("ContentProvider") 

local ImagesToPreload = {"http://www.roblox.com/asset/?id=9048610761", imageClone, "rbxassetid://9048610761"} -- array
ContentProvider:PreloadAsync(ImagesToPreload)


local scale = 40
local m = 1

clone.Size = UDim2.new(0, scale, 0, scale)
local i = 100 * (((100/m)/scale) * ((100/m)/scale))


local x = 500
local y = 500

local max = 500

local xf = 0
local yf = 0

local function paint(count)
    local clone = clone:Clone()
    
    local image = imageClone:Clone()
    clone.LayoutOrder = count
    clone.Parent = fr
    
    clone.Name = "Frame" .. count
    
    local r1 = math.random(2)
    local r2 = math.random(2)
    local offset1 = -math.random()
    local offset2 = -math.random()
    
    if r1 == 1 then
        clone.Position = clone.Position + UDim2.new(1,0,0,0)
        offset1 = math.abs(offset1)
    end
    if r2 == 1 then
        clone.Position = clone.Position + UDim2.new(0,0,1,0)
        offset2 = math.abs(offset2)
    end
    
    clone.Position = clone.Position + UDim2.new(offset1,0,offset2,0)
    
    clone:TweenPosition(UDim2.new(0,xf,0,yf), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 3)
    
    
    image.Parent = clone
    image.Position = UDim2.new(0,x,0,y)
    
    image.Image = "rbxassetid://9048610761"
    
    x = x - scale;
    if x <= -max then
        x = max;
        y = y - scale;
    end
    xf = xf + scale;
    if xf >= 2*max then
        xf = 0;
        yf = yf + scale;
        
        
    end
    

    end
for count = 1, i do
    paint(count)
    c = 0


Script.Parent.Parent:Destroy()
end

You’re destroying it in the for loop. Move it past that last “end”

edit: And what @trucker1737 said.

You made a tiny scripting error, but luau is case-sensitive. The error is you capitalized the S in script.Parent.Parent:Destroy(). Change the s to a lowercase one.

I have done this also fixed the case but it destroys before actually even running?

Can you send me your most current code? I’ll take a look at it and fix it for you.
I’ll of course leave comments so as to let you learn from it.

local fr = script.Parent
local clone = script.Frame
local imageClone = script.Logo
local Main = script.Parent.Parent
local RunService = game:GetService("RunService")
local connection
local ContentProvider = game:GetService("ContentProvider") 

local ImagesToPreload = {"http://www.roblox.com/asset/?id=9048610761", imageClone, "rbxassetid://9048610761"} -- array
ContentProvider:PreloadAsync(ImagesToPreload)


local scale = 40
local m = 1

clone.Size = UDim2.new(0, scale, 0, scale)
local i = 100 * (((100/m)/scale) * ((100/m)/scale))


local x = 500
local y = 500

local max = 500

local xf = 0
local yf = 0

local function paint(count)
	local clone = clone:Clone()
	
	local image = imageClone:Clone()
	clone.LayoutOrder = count
	clone.Parent = fr
	
	clone.Name = "Frame" .. count
	
	local r1 = math.random(2)
	local r2 = math.random(2)
	local offset1 = -math.random()
	local offset2 = -math.random()
	
	if r1 == 1 then
		clone.Position = clone.Position + UDim2.new(1,0,0,0)
		offset1 = math.abs(offset1)
	end
	if r2 == 1 then
		clone.Position = clone.Position + UDim2.new(0,0,1,0)
		offset2 = math.abs(offset2)
	end
	
	clone.Position = clone.Position + UDim2.new(offset1,0,offset2,0)
	
	clone:TweenPosition(UDim2.new(0,xf,0,yf), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 3)
	
	
	image.Parent = clone
	image.Position = UDim2.new(0,x,0,y)
	
	image.Image = "rbxassetid://9048610761"
	
	x = x - scale;
	if x <= -max then
		x = max;
		y = y - scale;
	end
	xf = xf + scale;
	if xf >= 2*max then
		xf = 0;
		yf = yf + scale;
		
		
	end
	

	end
for count = 1, i do
	paint(count)
	c = 0

end

script.Parent.Parent:Destroy()

Alright I’ll get back to you with an edit and an @ when I’m done.

Edit: @Axyanz, try this code and let me know if it works for your purpose:

local fr = script.Parent
local clone = script.Frame
local imageClone = script.Logo
local Main = script.Parent.Parent
local RunService = game:GetService("RunService")
local connection
local ContentProvider = game:GetService("ContentProvider")

local ImagesToPreload = {"http://www.roblox.com/asset/?id=9048610761", imageClone, "rbxassetid://9048610761"} -- array
ContentProvider:PreloadAsync(ImagesToPreload)

local scale = 40
local m = 1

clone.Size = UDim2.new(0, scale, 0, scale)
local i = 100 * (((100 / m) / scale) * ((100 / m) / scale))

local x = 500
local y = 500

local max = 500

local xf = 0
local yf = 0

-- Something to note:
--[[
    Roblox's LuaU has ternary operators. This means that "Variable += X" is valid LuaU.
]]--

local function paint(count)
  local new = clone:Clone()
  -- I suggest not using the same name as something out of scope.
  -- It's not going to cause harm but it can easily become confusing with longer code.

  local image = imageClone:Clone()
  new.LayoutOrder = count

  new.Name = "Frame" .. count

  local r1 = math.random(2)
  local r2 = math.random(2)
  local offset1 = -math.random()
  local offset2 = -math.random()

  if r1 == 1 then
    new.Position += UDim2.new(1, 0, 0, 0)
    offset1 = math.abs(offset1)
  end
  if r2 == 1 then
    new.Position += UDim2.new(0, 0, 1, 0)
    offset2 = math.abs(offset2)
  end

  new.Position += UDim2.new(offset1, 0, offset2, 0)

  image.Position = UDim2.new(0, x, 0, y)

  image.Image = "rbxassetid://9048610761"

  image.Parent = new -- Don't parent instances until you're
  -- done setting their properties.
  -- And their children for that matter.
  new.Parent = fr
  -- Start the tween now that it's ready.
  new:TweenPosition(UDim2.new(0, xf, 0, yf), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 3)

  x -= scale;
  if x <= -max then
    x = max;
    y -= scale;
  end
  xf += scale;
  if xf >= 2 * max then
    xf = 0;
    yf += scale;
  end
end

-- Run the for loop.
for count = 1, i do
  task.spawn(paint, count)
  -- Spawn them all in, likely won't cause any noticable performance gain
  -- If it doesn't, or causes lag, you can just revert to paint(count)
  c = 0
end

-- Delay by 6 seconds to make sure there's absolutely time to see it before we Destroy it.
task.delay(6, function()
    Main:Destroy()
end)
1 Like

Thank you this works, really appreciate it!

No problem. If you want it to stay around longer, you can just increase the “6” in the task.delay call at the bottom.