So im making a tycoon kit that uses a gui and when i click a button i want it to detect when its clicked and i want to do it in one script, but it dont work please help! heres the script
local Player = game.Players.LocalPlayer local Tycoon local Ls = Player.leaderstats local Cash = Ls.Cash for _,buttons in pairs(script.Parent.Background.Buttons:GetChildren())do buttons.MouseButton1Click:connect(function(button) print("A button was clicked") --[[if buttons.Bought.Value == false then if Cash.Value >= buttons.Cost.Value then local Tycoon = game.Workspace.TycoonKit:FindFirstChild(Player.Owned_Tycoon.Value) if Tycoon then local Item = Tycoon.Buyables:FindFirstChild(buttons.Item.Value) if Item then Item.Parent = Tycoon.Bought Cash.Value = Cash.Value - buttons.Cost.Value end end end end--]] end) end
But thats the part thats not working
buttons.MouseButton1Click:connect(function(button)
Before you do the click event you should make an if statement with :IsA() to check that the current buttons in the for loop is a TextButton or whatever so that the MouseButton1Click event is part of the instance. also theres no point in the button in (function(button) because it'll just be nil anyways.
Comment edit
local Player = game.Players.LocalPlayer local Tycoon local Ls = Player.leaderstats local Cash = Ls.Cash for _,buttons in pairs(script.Parent.Background.Buttons:GetChildren()) do if buttons:IsA("TextButton") then buttons.MouseButton1Click:connect(function() print("A button was clicked") end--]] end) end end