How do I stop this from happening? And make this happen?

Yo Alright, here’s the rundown

WHAT MY SCRIPT DOES

My script takes the decals that are inside of a folder, turns them into ImageButton’s, and shows them inside of my GUI

MY PROBLEMS

When I click to add the button to the workspace, here’s my two problems:

When I click, my script detects if a screen gui is inside of StarterGui, and if it isn’t, then it will add in the screen gui along with the button, and if it is then it will parent it to the screengui, but my problem is that I click to add and everything works, but it just creates another screengui with nothing inside of it, which clutters up my StarterGUI

Capture

My second problem is that when I click the button, it shows up in StarterGUI, but doesn’t show up in-game.

It’s supposed to be in the top right hand corner! I already checked the properties, and that’s not the problem.

Capture
Capture

MY SCRIPT

local scrollingFrame = script.Parent
local decals = script.Parent.Decals

for index, value in pairs(decals:GetChildren()) do
	if value:IsA("Decal") then
		local iButton = Instance.new("ImageButton")
		iButton.Image = value.Texture
		iButton.Parent = scrollingFrame
		iButton.Size = UDim2.new(0.1, 0, 0.1, 0)
		iButton.Transparency = 1
		iButton.Visible = true
		
		iButton.MouseButton1Click:Connect(function()
			local screengui = Instance.new("ScreenGui")
			screengui.Parent = game.StarterGui
			screengui.Name = "Interface Library V1.0.0 - LITE - Inserted Elements"
			
			if game.StarterGui["Interface Library V1.0.0 - LITE - Inserted Elements"] then
				
				local button = Instance.new("ImageButton")
				button.AnchorPoint = Vector2.new(0.5, 0.5)
				button.AutoButtonColor = false
				button.BackgroundTransparency = 1
				button.Name = "Inserted Element"
				button.Parent = game.StarterGui["Interface Library V1.0.0 - LITE - Inserted Elements"]
				button.Position = UDim2.new(0.9, 0, 0.5,0)
				button.Size = UDim2.new(0.098, 0,0.158, 0)
				button.Image = iButton.Image
				button.ScaleType = "Fit"
				
			else
			
				local button = Instance.new("ImageButton")
				button.AnchorPoint = Vector2.new(0.5, 0.5)
				button.AutoButtonColor = false
				button.BackgroundTransparency = 1
				button.Name = "Inserted Element"
				button.Parent = screengui
				button.Position = UDim2.new(0.9, 0, 0.5,0)
				button.Size = UDim2.new(0.098, 0,0.158, 0)
				button.Image = iButton.Image
				button.ScaleType = "Fit"
			end
		end)
	end
end

By chance did I miss anything or do something wrong? Let me know.

Right here you are creating a new screenGui on MouseButton1Click. You should change it to,

local screengui = game.StarterGui:FindFirstChildWhichIsA("ScreenGui") or Instance.new("ScreenGui")

Alternatively, you can do :FindFirstChild("ScreenGuiNameHere") instead of :FindFirstChildWhichIsA("ScreenGui")

Alright, you gottit!

You know anything about the other problem?

add the stuff to playergui not startergui
PlayerGui Doc

keep in mind every player has their own PlayerGui

-- Accessing PlayerGui from a LocalScript:
game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui')

this is code from the docs

And your solution worked too! Thanks both of you!

btw I know this wasn’t part of the question, but I thought I would help improving the code a bit for you

here is a version of the code where you only use an if statement for the parts that are different
local button = Instance.new("ImageButton")
button.AnchorPoint = Vector2.new(0.5, 0.5)
button.AutoButtonColor = false
button.BackgroundTransparency = 1
button.Name = "Inserted Element"

if game.StarterGui["Interface Library V1.0.0 - LITE - Inserted Elements"] then
    button.Parent = playergui
else
    button.Parent = screengui
end

button.Position = UDim2.new(0.9, 0, 0.5,0)
button.Size = UDim2.new(0.098, 0,0.158, 0)
button.Image = iButton.Image
button.ScaleType = "Fit"
alternative version with function
local function CreateButton(Parent)
    local button = Instance.new("ImageButton")
    button.AnchorPoint = Vector2.new(0.5, 0.5)
    button.AutoButtonColor = false
    button.BackgroundTransparency = 1
    button.Name = "Inserted Element"
    button.Parent = Parent
    button.Position = UDim2.new(0.9, 0, 0.5,0)
    button.Size = UDim2.new(0.098, 0,0.158, 0)
    button.Image = iButton.Image
    button.ScaleType = "Fit"
end

if game.StarterGui["Interface Library V1.0.0 - LITE - Inserted Elements"] then
    CreateButton(playergui)
else
    CreateButton(screengui)
end

obviously you might have to change some stuff like replacing “playergui” with your solution, but this is a great way to reduce all the repeated code!

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