[ENGINE BUG SOLUTION] InputBegan ZIndex Prioritization

Recently I experienced with InputBegan firing on sank GUI’s, and I didn’t find a solution to it, but instead made one.

local module = {}
local Inputed = {}
local CurrentIndex = 0
local OnTop = nil

module.Connect = function(object, callback)
	object.InputBegan:Connect(function(inp)
		if inp.UserInputType == Enum.UserInputType.MouseButton1 then	
			table.insert(Inputed, object)
			
			wait()
			
			for _, ob in pairs(Inputed) do
				if ob.ZIndex > CurrentIndex then	
					CurrentIndex = ob.ZIndex
					OnTop = ob
				end
			end
			
			wait()
			
			if OnTop == object then	
				callback(inp)
			end
			
			CurrentIndex = 0
			OnTop = nil
			Inputed = {}
		end
	end)
end

return module

Just make module script, put this in, require it, and use it.
(This is only for M1, feel free to change)
Usage:

local Input = require(module)
local Frame = script.Parent
Input.Connect(Frame, function(inp)
        if not inp.UserInputType == Enum.UserInputType.MouseButton1 then return end
        print("Clicked")
end)

Also Roblox, from this solution, I expect a one million dollar payment for this solution.
My bank: LT9218294873349

obviously a joke

4 Likes

Why go through the trouble of exporting a table if it only has one function?

prob so you dont need to create same function in every script, and just to require and use the function on module?

I mean using the

return function()
    …
end

syntax.

cause i had to define some variables before, and they cannot be defined inside the function cause then the function will not work as it should

You can still define variables outside the function scope.

local Indexed = {}
return function()
    table.insert(Indexed, “test”)
end
1 Like
local HttpService = game:GetService("HttpService")
local module = {}
local Regs = {}

module.Register = function()
Regs[HttpService:GenerateGUID(false)] = {}
end

module.Connect = function(object, callback)
for Id, _d in Regs do
if typeof(_d) == "table" then
local private = {}
local Inputed = {}
local CurrentIndex = 0
local OnTop = nil
	_d = object.InputBegan:Connect(function(inp)
		if inp.UserInputType == Enum.UserInputType.MouseButton1 then	
			table.insert(Inputed, object)
			
			for _, ob in pairs(Inputed) do
				if ob.ZIndex > CurrentIndex then	
					CurrentIndex = ob.ZIndex
					OnTop = ob
				end
			end
			
			if OnTop == object then	
				task.spawn(callback, inp)
			end
			
			CurrentIndex = 0
			OnTop = nil
			Inputed = {}
		end
	end)
private.Disconnect = function()
if Regs[Id] and _d then
_d:Disconnect()
Regs[Id] = nil
end
end
return table.freeze(private)
end
warn("Failed to connect, Have you Registered a new?")
end

return table.freeze(module)

sorry if the code not format cuz i wrote it on phone. i added Register so you can use multiple times and Disconnecting but i havent test it.

1 Like

I think you confused push with insert. Great js coding eh?

2 Likes

usage:

module.Register() – register new
module.Connect(instance, function(…)

end)
– you can repeat or register new for a new usage

this is so unneeded
u can already connect to multiple instances, thats the point for overlap detection