| ButtonHandler | Open-Source Script!

so recently ive been improving my CKIT, and got a Killbrick script to manage all killbricks.

But that was using game.World.Each… which this also uses… Query is included.

I’ve improved the script a lot since i first posted it here, now i wanna share it with you all!

Make sure to credit me if you do use this script though.

Here’s the ButtonHandler of my CKIT! Manage all those pesky buttons with one script.

-- man Jediweirdo is a hero! made by Cva!

local Buttons = game.World.Query(game.World.Pair("Tag", "ButtonObject"))
local ButtonOrigin = {}
local ButtonDeactivators = game.World.Each(game.World.Pair("Tag", "ButtonDeactivator"))
local ButtonActivatedParts = game.World.Each(game.World.Pair("Tag", "ButtonActivatedPart"))
local isInitialized = false



local function ActivateParts(Color)
	for i in ButtonActivatedParts do
		local IColor = i.attr.ColorOverride or i.Color
		local PTrans = 0
		if IColor == Color or Color == false then
			if i.attr.Invert then
				PTrans = 0.5
				if i.attr.FullHide then
				PTrans = 1
				end
			end
		--i:AddComponent("Transparency")
		--i:AddComponent("Collider")
		i.Transparency = i.attr.SetTransparency or PTrans
		i.Collider = i.attr.Invert and "None" or "Auto"
		end
	end
end

local function DeactivateParts(Color)
	ButtonActivatedParts = game.World.Each(game.World.Pair("Tag", "ButtonActivatedPart"))
	for i in ButtonActivatedParts do
		local IColor = i.Color
		local PTrans = 0.5
		if i.attr.ColorOverride then		
		 IColor = i.attr.ColorOverride
		end
		if IColor == Color or Color == false then
			if i.attr.FullHide then
				PTrans = 1
			end
			if i.attr.Invert then
				PTrans = 0
			end
			i.Collider = i.attr.Invert and "Auto" or "None"
			i.Transparency = i.attr.IgnoreTransparency or PTrans
		end
	end
end

local function ButtonAlter(ButtonObject, Origin, Material)
	local Button = ButtonObject:GetHandle('Button')
	if Origin == false then
	Button.Position += Button:TransformDirection(Vector3.new(0,-0.5,0))
	else	
	Button.Position = Origin
	end
	Button.Material = Material or "Neon"

end

local function DeactivateButtons(Color)
	for I = 1, Buttons.count do
		local ButtonObject = Buttons.Instance[I]
		local Button = ButtonObject:GetHandle('Button')
		local ButtonColor = Button.Color
		local O = Buttons.Instance[I]:GetHandle("Properties")
		if not O then
			warn(ButtonObject,"ButtonObject is missing a Properties Handle!")
			O = ButtonObject
		end
		local ButtonTimer = O:FindFirstChild("ButtonTimer")
			if ButtonObject.attr.Pressed == true and ButtonColor == Color or Color == false then
				if ButtonTimer then
				if ButtonTimer:HasComponent("TimerRunning") then
					ButtonTimer:RemoveComponent("TimerRunning")
					print("ButtonTimer Disconnected!")
				end
				end
			ButtonAlter(ButtonObject, ButtonOrigin[I], "SmoothPlastic")
			DeactivateParts(Color)
			end
	end
end



print("RUNNING")
function Update()
	
	if isInitialized ~= true then
	Buttons:Refresh()
	
for I = 1, Buttons.count do
	local Button = Buttons.Instance[I]:GetHandle('Button')
	local ButtonObject = Buttons.Instance[I]
	ButtonOrigin[I] = Button.Position
	local Color = Button.Color
	local O = Buttons.Instance[I]:GetHandle("Properties")
	if not Button or not O then
		if not O then
			warn(ButtonObject, "is missing Properties")
			O = ButtonObject
		else
		warn("Critical Error,", ButtonObject, "will not work due to lack of Button")
		end
	end
	local ColorSensitive = O.attr.ColorSensitive
	if not ColorSensitive then
		Color = false
	end
	local ButtonTimer = O:FindFirstChild("ButtonTimer")
	
	Button.Touched:Connect(function(hit: Instance)
		print(hit.Name,"touched button")
		if Button.Material ~= "Neon" then
		ButtonActivatedParts = game.World.Each(game.World.Pair("Tag", "ButtonActivatedPart"))
		ButtonObject.attr.Pressed = false
		end
		if hit:HasComponent("BodyPart") == true and ButtonObject.attr.Pressed ~= true and O.attr.AcceptPlayers then
			ButtonAlter(ButtonObject, false)
			ButtonObject.attr.Pressed = true
				ActivateParts(Color)
				if ButtonTimer then
				ButtonTimer:AddComponent("TimerRunning")
				end

		elseif hit:HasComponent(game.World.Pair("Tag", "Pushbox")) and ButtonObject.attr.Pressed ~= true and O.attr.AcceptPushbox and hit.Color == Color then
			ButtonAlter(ButtonObject, false)
			ButtonObject.attr.Pressed = true
				ActivateParts(Color)
				if ButtonTimer then
				ButtonTimer:AddComponent("TimerRunning")
				end
				
		elseif hit:HasComponent(game.World.Pair("Tag", "Pushbox")) and ButtonObject.attr.Pressed ~= true and O.attr.AcceptPushbox and O.attr.BoxColorIgnore then
			ButtonAlter(ButtonObject, false)
			ButtonObject.attr.Pressed = true
				ActivateParts(Color)
				if ButtonTimer then
				ButtonTimer:AddComponent("TimerRunning")
				end
		else
		warn("Cant find it! / Pressed / Not accepted! / Didn't match!")
	end
end)
if ButtonTimer then
	ButtonTimer.Timeout:Connect(function()
		ButtonAlter(ButtonObject, ButtonOrigin[I], "SmoothPlastic")
		DeactivateParts(Color)
		ButtonTimer:RemoveComponent("TimerRunning")
		print("ButtonTimer Ended!")
end)
end
end
for i in ButtonDeactivators do
	local Color = i.Color
	local ColorSensitive = i.attr.ColorSensitive
	if not ColorSensitive then
		Color = false
	end
	i.Touched:Connect(function(hit: Instance)
	if hit:HasComponent("BodyPart") == true then
		DeactivateButtons(Color)
	else
		warn("Cant find character!")
	end
end)
isInitialized = true
end


end
end

i am not to be held liable for any ghost parts that appear due to the ButtonHandler.

thanks @Jediweirdo for inspiring me to make this script.

i suppose a video might help with explaining the errors…

why does pressing the button push the pushboxes??? and i guess you can see the color issue here too…

im bumping this as the essence of the topic has changed.

Feel free to ask me things if you run into a problem.

Outdated

Bro I genuinely forgot about this :sob:. I ignored it at first on purpose because I didn’t have a functioning computer until last night to actually look at code in-depth with, but I forgot afterward to come back to this.

And now that I’m looking at this… yeah this is a bit of a mess. So much so that I just am not going to try to fix the bugs how it is now and am basically going to redo giant parts of this to make it look more ledgible. While I rip everything out and groom it, I’ll walk you through what I changed:

  1. If you only want the stuff in the update loop to run once, then… don’t put it in an update loop that runs every frame. I don’t know if it was because if a Luduvo bug it was like that or something, but I just got rid of it and the isInitialized boolean
  2. MassRefresh is a 1 line function that is more of a pain to find than it is to just type out Button:Refresh(). Also gone
  3. I see that you used a query… but then used it the exact same as game.World.Each? I don’t know the story there, but its game.Query.Each now like the rest of your queries.
  4. Some of your variable names are poopoo dog water. Because the original code had a mix of queries and normal instance for loops, I kept confusing i the instance with i the number. War crimes.
  5. I’m assuming ButtonOrigin exists to put the buttons back to how they were after they got pressed. But you can also just use Button:TransformDirection(Vector3.new(0,-0.5,0)) but in reverse instead. So… I did that.
  6. In Activate/Deactivate, there’s a lot of copy/pasted code that could just be 1 block of code again if you flip the conditions so that only false things will go inside the if block and then replace its contents with return so that it just stops iterating. Basically the difference between:
if a == b then
 print("yipee")
 print("yipee")
 print("yipee")

elseif b == c then
 print("yipee")
 print("yipee")
 print("yipee")

elseif --...
end

and

if a ~= b or b ~= c then
 return
end

print("yipee")
print("yipee")
print("yipee")
  1. There’s no point in doing AddComponent if you are also using the flattened instance API to assign it. The flattened API will automatically add the components for you
  2. You use GetHandle a lot, but you never do any checks to make sure the handle actually exists before using it
  3. Some of your functions compeltely forget about ColorOverride and just use the normal color anyway, like deactivateParts and the .Touched stuff
  4. You reassign ButtonActivatedParts a ton in the code for seemingly no reason. Does a different script add/change activated parts or something?
  5. You never reset pressed when you press the reset button, but check if the part is neon instead…? Again, I’m not sure if there was a story behind that or not, but I removed it too

This is the final code I came up with:

-- man Jediweirdo is a hero!

local Buttons = game.World.Each(game.World.Pair("Tag", "ButtonData"))
local ButtonDeactivators = game.World.Each(game.World.Pair("Tag", "ButtonDeactivator"))
local ButtonActivatedParts = game.World.Each(game.World.Pair("Tag", "ButtonActivatedPart"))

local function ActivateParts(Color)
	for Part in ButtonActivatedParts do
		local IColor = Part.attr.ColorOverride or Part.Color -- defaults to Part.Color if ColorOverride is nil
		if IColor ~= Color and Color ~= false then
			continue
		end

		local PTrans = 0
		if Part.attr.Invert then
			PTrans = 0.5
		end
		if Part.attr.FullHide then
			PTrans = 1
		end
		Part.Transparency = Part.attr.SetTransparency or PTrans
		Part.Collider = Part.attr.Invert and "None" or "Auto" -- 1 liner if statement. The "and" part is the truthy and the "or" part is the falsy
	end
end

local function DeactivateParts(Color)
	print(Color, "Color")

	for Part in ButtonActivatedParts do
		local IColor = Part.attr.ColorOverride or Part.Color
		if IColor ~= Color and Color ~= false then
			continue
		end

		Part.Collider = Part.attr.Invert and "Auto" or "None"

		local PTrans = 0.5
		if Part.attr.FullHide then
			PTrans = 1
		end
		if Part.attr.Invert then
			PTrans = 0
		end
		Part.Transparency = Part.attr.IgnoreTransparency or PTrans
	end
end

local function DeactivateButtons(Color)
	for ButtonData in Buttons do
		local Button = ButtonData:GetHandle('Button')
		if not Button then
			continue
		end

		local IColor = Button.attr.ColorOverride or Button.Color
		if not ButtonData.attr.Pressed or (IColor ~= Color and Color ~= false) then
			continue
		end

		ButtonData.attr.Pressed = false
		Button.Position -= Button:TransformDirection(Vector3.new(0,-0.5,0))
		Button.Material = "SmoothPlastic"
		DeactivateParts(Color)
	end
end

for ButtonData, _ in Buttons do
	print(ButtonData, "ButtonData")

	local Button = ButtonData:GetHandle('Button')
	local ButtonPart =  ButtonData:GetHandle('ButtonPart')
	if not Button or not ButtonPart then
		continue
	end

	local IColor = Button.attr.ColorOverride or Button.Color
	print(ButtonData.Name, "UpdateF")

	ButtonPart.Touched:Connect(function(hit: Instance)
		print(hit,"touched button")
		local HitColor = hit.attr.ColorOverride or hit.Color
        if ButtonData.attr.Pressed or not hit:HasComponent(game.World.Pair("Tag", "ButtonActivatedPart")) and not hit:HasComponent("BodyPart") then
            return
        end
        if hit:HasComponent("BodyPart") and not ButtonData.attr.AcceptPlayers then
            return
        end
        if hit:HasComponent(game.World.Pair("Tag", "Pushbox")) and not ButtonData.attr.AcceptPushbox then
            return
        end
        if hit:HasComponent(game.World.Pair("Tag", "Pushbox")) and (HitColor ~= IColor and not ButtonData.attr.BoxColorIgnore) then
            return
        end

       	Button.Material = "Neon"
       	Button.Position += Button:TransformDirection(Vector3.new(0,-0.5,0))
       	ButtonData.attr.Pressed = true
        ActivateParts(ButtonData.attr.ColorSensitive and IColor or false)
    end)

end

for Button in ButtonDeactivators do
	local ButtonDeactivator = Button:GetHandle('ButtonDeactivator')
	if not ButtonDeactivator then
	    return
	end

	ButtonDeactivator.Touched:Connect(function(hit: Instance)
		print(hit,"deactivated button")
		if not hit:HasComponent("BodyPart") then
			return
		end

		DeactivateButtons(Button.attr.ColorSensitive and ButtonDeactivator.Color or false)
	end)
end

edit: I realized I had a bug and forgot to flip an or to an and

Congrats! My bad for not responding before you had to basically do everything yourself, I didn’t have a computer up until recently and me solving the original problem would have required me to be able to playtest and whatnot. I’ll still leave what I was working on yesterday on and off in a summary drawer, but its probably not relevant anymore

1 Like

MassRefresh() was a remnant of when i had 3 queries lol