Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

When i click this button it says it cant be found?

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)

1 answer

✓ Accepted answer

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

14 comments

0

Then how do i do it with only one script in a simple way?

0

Then how do i do it with only one script in a simple way?

0

@EndorsedScripter ;-;.. edited answer

0

oh okay, ill try it

0

Okay, how do i detect which button was click?

0

@EndorsedScripter You could check it's name in an if statement. buttons.Name

0

is there a easier name that can just get the info of it in the button without having to do a bunch of if statments

0

way

0

i ment way not name

1

uh whats the problem, if theres something inside that textbutton you want you could just do buttons.thingyouwant and make sure its there stuff like that

0

@EndorsedScripter

0

Okay ill try that

0

Also, the MouseButton1Click event does not have a parameter

0

Alright it worked thanks :D

This discussion is preserved as it was. New answers, comments, and votes are closed.