So I made a client-server system for detecting button click on SurfaceGUIs.
It works properly if there’s one instance of the model the script is in, but if there’s more only the first one added receives btnActivated.
Server
function f.onClick(button:GuiButton,func)
if f.root.Name == "NerdTV2" then
print("connected") --prints
end
if not button:IsA("GuiButton") then error("Not a button") end
if (typeof(func) ~= "function") and (typeof(func) ~= "BindableFunction") then error("Not a function or BindableFunction") end
local con = f.c2s.btnActivated.OnServerEvent:Connect(function(plr,btn:GuiButton)
if f.root.Name == "NerdTV2" then
print("worked") --does not print
end
if not btn then return end
if button ~= btn then return end
if not f.utils.isVisibleInTree(button) then return false end
if typeof(func) == "function" then
func(plr)
elseif typeof(func) == "BindableFunction" then
func:Fire(plr)
end
end)
return con
end
Client
function connect(btn:GuiButton)
if not btn then return end
if not btn:IsA("GuiButton") then return end
if table.find(connected,btn) then return end
table.insert(connected,btn)
btn.Activated:Connect(function()
c2s.btnActivated:FireServer(btn) -- i tested it before, always fires
end)
end
The original model is named NerdTV and the copy is named NerdTV2.
Both use the same c2s (stands for client to server) instance in ReplicatedStorage.