Script not triggering when button is pressed

I’m genuinely losing sanity over this because I’ve literally made no dumb mistake here.

I’m trying to optimise my plugin auroraSuite in a move that should save a tenth of a MB of space when installing (with an RBXMX file), as well as making editing stuff in the future.

One of the steps I’m trying to accomplish this is by making multiple ImageButtons in the plugin that are generated via a script, rather than just bundling it via RBXMX.

Here’s the relevant folder structure:

image

The ImageButtons that are currently there are ones that aren’t being generated by the generator. They work perfectly fine and cause no conflicts with the ones I’m trying to generate using the generator.

Here is the code inside the generator script, this runs successfully on plugin launch, and my issues don’t arise from this, but with a script that the generator creates inside every ImageButton.

-- When this is completed, it'll save time by allowing me to add numerous icons on launching the plugin rather than baking it into the plugins file

local par = script.Parent

local icons = {
	["home filled"] = "http://www.roblox.com/asset/?id=12815564761",
	["home outlined"] = "http://www.roblox.com/asset/?id=12815570718",
	["backpack filled"] = "http://www.roblox.com/asset/?id=12815630414",
	["backpack outlined"] = "http://www.roblox.com/asset/?id=12815641161",
	["trolley filled"] = "http://www.roblox.com/asset/?id=12816051676",
	["trolley outlined"] = "http://www.roblox.com/asset/?id=12816058954",
	["user filled"] = "http://www.roblox.com/asset/?id=12874654142",
	["user outlined"] = "http://www.roblox.com/asset/?id=12874675901",
	["left filled"] = "http://www.roblox.com/asset/?id=13683469925",
	["left outlined"] = "http://www.roblox.com/asset/?id=13683473826",
	["right filled"] = "http://www.roblox.com/asset/?id=13683477457",
	["right outlined"] = "http://www.roblox.com/asset/?id=13683481669",
	["x filled"] = "http://www.roblox.com/asset/?id=14180288939",
	["x outlined"] = "http://www.roblox.com/asset/?id=14180278322",
	["keyboard"] = "http://www.roblox.com/asset/?id=13683485207"
}

for name, assetid in pairs(icons) do
	local icon = Instance.new("ImageButton", par)
	
	icon.Name = name
	icon.Image = assetid
	icon.BackgroundTransparency = 1
	icon.BorderSizePixel = 1
	icon.Size = UDim2.new(0,16,0,16)
	icon.Visible = true
	
	local iconscript = Instance.new("Script", icon)
	
	iconscript.Name = "Activation"
	iconscript.Source = [[
script.Parent.MouseButton1Click:Connect(function()
	print("its running")
	local selection = game:GetService("Selection")
	local history = game:GetService("ChangeHistoryService")
	local selected = selection:Get()
	local tag = game:GetService("CollectionService")

	if #selected == 0 then
		warn("auroraSuite: Selection empty, make sure to select an ImageLabel, ImageButton or Decal. ")
		return
	end
	
	local record = history:TryBeginRecording("auroraSuite: Insert icon")
	
	if record then
		for i, v in selected do
			if v.ClassName == "ImageLabel" or v.ClassName == "ImageButton" then
				v.Image = script.Parent.Image
				if plugin:GetSetting("DebugMode") == true then
					print("auroraSuite: Inserted icon into " .. v:GetFullName())
				end
				tag:AddTag(v, "auroraSuite-icon")
			else if v.ClassName == "Decal" then
					v.Texture = script.Parent.Image
					if plugin:GetSetting("DebugMode") == true then
						print("auroraSuite: Inserted icon into " .. v:GetFullName())
					end
					tag:AddTag(v, "auroraSuite-icon")
				end
			end
		end
	end
	
	history:FinishRecording(record, Enum.FinishRecordingOperation.Commit)
end)
	]]
	
	task.wait(0.01)
end

When launching, both the old ones that weren’t generated with the generator and the new ones that were generated using the generator show up just fine (the generator ones are the ones that appear for the second time).

image

I’m using a slight deviation of the script used in the generated by script and non generated by script icons, but the slight deviation works fine if I paste it into the non generated ones.

Here’s the script inside each of the generated buttons:

script.Parent.MouseButton1Click:Connect(function()
	print("its running")
	local selection = game:GetService("Selection")
	local history = game:GetService("ChangeHistoryService")
	local selected = selection:Get()
	local tag = game:GetService("CollectionService")

	if #selected == 0 then
		warn("auroraSuite: Selection empty, make sure to select an ImageLabel, ImageButton or Decal. ")
		return
	end
	
	local record = history:TryBeginRecording("auroraSuite: Insert icon")
	
	if record then
		for i, v in selected do
			if v.ClassName == "ImageLabel" or v.ClassName == "ImageButton" then
				v.Image = script.Parent.Image
				if plugin:GetSetting("DebugMode") == true then
					print("auroraSuite: Inserted icon into " .. v:GetFullName())
				end
				tag:AddTag(v, "auroraSuite-icon")
			else if v.ClassName == "Decal" then
					v.Texture = script.Parent.Image
					if plugin:GetSetting("DebugMode") == true then
						print("auroraSuite: Inserted icon into " .. v:GetFullName())
					end
					tag:AddTag(v, "auroraSuite-icon")
				end
			end
		end
	end
	
	history:FinishRecording(record, Enum.FinishRecordingOperation.Commit)
end)
	

image

Why does it not activate on the ones generated with the script? I can look into alternative solutions if need be, but there’s no reason for it not to activate in its current state.

Here is the full plugin file for anyone to tinker with:
aurorasuite.rbxmx (1.0 MB)

2 Likes

Instead of creating a new script for each button, why don’t you just connect the functions instead? The only reason to make something like this is to eliminate the need for a script in each button.

Fixed code for generator:

--//Services
local selection = game:GetService("Selection")
local history = game:GetService("ChangeHistoryService")
local CollectionService = game:GetService("CollectionService")

--//Variables
local par = script.Parent

--//Tables
local icons = {
	["home filled"] = "http://www.roblox.com/asset/?id=12815564761",
	["home outlined"] = "http://www.roblox.com/asset/?id=12815570718",
	["backpack filled"] = "http://www.roblox.com/asset/?id=12815630414",
	["backpack outlined"] = "http://www.roblox.com/asset/?id=12815641161",
	["trolley filled"] = "http://www.roblox.com/asset/?id=12816051676",
	["trolley outlined"] = "http://www.roblox.com/asset/?id=12816058954",
	["user filled"] = "http://www.roblox.com/asset/?id=12874654142",
	["user outlined"] = "http://www.roblox.com/asset/?id=12874675901",
	["left filled"] = "http://www.roblox.com/asset/?id=13683469925",
	["left outlined"] = "http://www.roblox.com/asset/?id=13683473826",
	["right filled"] = "http://www.roblox.com/asset/?id=13683477457",
	["right outlined"] = "http://www.roblox.com/asset/?id=13683481669",
	["x filled"] = "http://www.roblox.com/asset/?id=14180288939",
	["x outlined"] = "http://www.roblox.com/asset/?id=14180278322",
	["keyboard"] = "http://www.roblox.com/asset/?id=13683485207"
}

--//Functions
local function CreateIcon(name, assetid)
	local icon = Instance.new("ImageButton")
	icon.Name = name
	icon.Image = assetid
	icon.BackgroundTransparency = 1
	icon.BorderSizePixel = 1
	icon.Size = UDim2.new(0, 16, 0, 16)
	icon.Visible = true
	icon.Parent = par
		
	icon.MouseButton1Click:Connect(function()
		local selected = selection:Get()

		if #selected == 0 then
			warn("auroraSuite: Selection empty, make sure to select an ImageLabel, ImageButton or Decal. ")
			return
		end

		local record = history:TryBeginRecording("auroraSuite: Insert icon")
		
		--//Failed to record, do not complete actions because they will not be recorded
		if not record then
			return
		end

		for i, v in selected do
			if v.ClassName == "ImageLabel" or v.ClassName == "ImageButton" then
				v.Image = icon.Image
				
				if plugin:GetSetting("DebugMode") then
					print("auroraSuite: Inserted icon into " .. v:GetFullName())
				end
				
				CollectionService:AddTag(v, "auroraSuite-icon")
			elseif v.ClassName == "Decal" then
				v.Texture = icon.Image
				
				if plugin:GetSetting("DebugMode") then
					print("auroraSuite: Inserted icon into " .. v:GetFullName())
				end
				
				CollectionService:AddTag(v, "auroraSuite-icon")
			end
		end
	
		history:FinishRecording(record, Enum.FinishRecordingOperation.Commit)
	end)
	
	print("Initialized ".. icon.Name)
end

for name, assetid in pairs(icons) do
	CreateIcon(name, assetid)
	
	--//CreateIcon will already yield until every process inside is finished, so I don't think task.wait() is necessary, remove it if possible
	task.wait()
end

Tell me if this works.

1 Like

I’ll try this later today (in like 19 hours lol) and see how it goes, thank you

2 Likes

No problem, I also fixed some logic issues like :FinishRecording, your previous script did :FinishRecording even if :TryBeginRecording wasn’t successful, which is not good.

1 Like

Works perfectly. I still have no idea why it didn’t work for me before, but it does now.

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