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

Function inside an if statement?

I have tried the below and there is an error to getting it to get the last number or something like that. Any help?

local booth = game.Workspace.CreditBooth.Board

local player = game.Players.LocalPlayer

local items1 = {booth.Side1.Credits:WaitForChild("10Credits"),booth.Side2.Credits:WaitForChild("10Credits"),booth.Side3.Credits:WaitForChild("10Credits"),booth.Side4.Credits:WaitForChild("10Credits")}

for _, v in pairs (items1) do
    v.MouseButton1Click:connect(function()
        local num = tonumber(string.sub(v, string.len(v))) -- Error is here.
        local parentnum = tonumber(string.sub(v.Parent, string.len(v.Parent)))
        if num == 1 and parentnum == 1  then

        elseif num == 1 and parentnum == 2 then

        end
    end)
end

Players.Player.PlayerGui.CreditBoothScript:11: bad argument #1 to 'len' (string expected, got userdata)

3 answers

✓ Accepted answer

Something like this?

local items = {script.Parent, script.Parent.Parent} -- just temporary

for _, v in pairs (items) do -- make the event for each of them
	v.MouseButton1Click:connect(function()
		print(v.Name.." was clicked")
		local num = tonumber(string.sub(v, string.len(v))) -- get the last letter and turn it into a number
		local parentnum = tonumber(string.sub(v.Parent, string.len(v.Parent)))
		if num == 1 and parentnum == 1  then
			
		elseif num == 1 and parentnum == 2 then
			
		end
	end)
end

3 comments

0

i was thinking about a for loop, but I need to be real shprt

0

I have editted my answer with your one, but there is an error. I changed it a bit to how I would like it. I have output and all

0

nvm, i solved it thanks :D

What you could do is cross-check a >boolean> when executing the >event> code if you're asking how to execute an event only if it's executed another. But, in this case, you can just use two separate events.

For example:

script.Parent.MouseButton1Click:connect(function()
print'1';
end);
script.Parent.Parent.MouseButton1Click:connect(function()
print'1';
end);

2 comments

0

yes, this does work, but my code is too long. I've got 4 guys that are the same, and want it go and check if a button is clicked on the first, or the second, or the third, etc. I don't want to just do a function for EVERy single one, else it would be a 10,000 line piece of code

0

Well, you could store the print'1' within a function and reduce the code to: function dis() print'1'; end; script.Parent.MouseButton1Click:connect(dis); script.Parent.Parent.MouseButton1Click:connect(dis);

local root_folder = script.Parent
-- Direct root_folder to your ScreenGui
local clickable = {TextButton = true, ImageButton = true}
local last_clicked

local function root(object)
	for i,v in pairs(object:GetChildren()) do 
		if clickable[v.ClassName] then 
			v.MouseButton1Click:connect(function()
				print(last_clicked and last_clicked.Name.. " is opened." or  "Opened "..v.Name.. " for the first time")
				-- do something
				last_clicked = v
			end)
		end
		root(v)
	end
end

root(root_folder)

5 comments

0

does it have to be that long? and can the variables be left out, so it make a bit more sense than root_folder? I'm hoping for something small and not long

0

The point of root_folder is so that it can index all your buttons without you having to manually reference each button. However, if you only have two buttons, just go with the guy below me.

0

i have about 10

0

and theres 4 frames, so its 10x4 so 40 buttons. I want it check if button1 on frame1 or button 1 on frame2 or button 1 on frame3 or button 1 on frame4 is pressed thendo something. Kinda like TMM's credit booth

0

Don't just post code! Explain your answers in detail!

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