Button system: spawn function

So, I did a trick (made up by me) called, “The Printing Advice” where you replace everyline after with a print command. For example:

script.Parent.MouseButton1Click:connect(function()
print("TPA")
script.Parent.Visible = false
print("TPA")
wait(1)
print("TPA")
script.Parent.Visible = true
print("TPA")
end)

When I press play, I see how much printed lines there are and if it shows like 3 I can see the 4th print and check what’s wrong. But in this case, there is a problem. I am using Flood escape 2’s button function. One line of code went wrong. I used my trick, and they only printed 6 lines of “tpa”. Here is the script:

local function check(m)
	print("tpa")
	local cbutton = m.Buttons:FindFirstChild("Button_"..currentbutton.Value)
	print("tpa")
	if cbutton:FindFirstChild("_Fade") then
		print("tpa")
		spawn(function()
		print("tpa")	
		fade(m)
		print("tpa")
		end)
	print("tpa")	
	elseif cbutton:FindFirstChild("_Appear") then
		print("tpa")
		spawn(function()
			print("tpa")
		appear(m)
		print("tpa")
		end)
		print("tpa")
	elseif cbutton:FindFirstChild("_Unanchor") then
		print("tpa")
		spawn(function()
			print("tpa")
		appear(m)
		print("tpa")
		end)
	end
end
1 Like

Is…

cbutton:FindFirstChild("_Appear")

ever false?

if not then your issue could be that your elseifs will only happen if

if cbutton:FindFirstChild("_Fade") then

is false

Is there a solution to this problem?

Edit: It depends. It could have a value that is inside of a button.

I’m not entirely sure what you are trying to do ( achieve overall) but if you were to replace your elseifwith normal If statements then that should fix the problem:

Example
local function check(m)
	print("tpa")
	local cbutton = m.Buttons:FindFirstChild("Button_"..currentbutton.Value)
	print("tpa")
	if cbutton:FindFirstChild("_Fade") then
		print("tpa")
		spawn(function()
		print("tpa")	
		fade(m)
		print("tpa")
		end)
	print("tpa")	
end
	if cbutton:FindFirstChild("_Appear") then
		print("tpa")
		spawn(function()
			print("tpa")
		appear(m)
		print("tpa")
		end)
		print("tpa")
   end
	if cbutton:FindFirstChild("_Unanchor") then
		print("tpa")
		spawn(function()
			print("tpa")
		appear(m)
		print("tpa")
		end)
	end
end

I don’t know if I’m a fan of ‘The Printing Advice’… Sure, you can see how many lines it executes, but you can’t see where your code actually goes. Wouldn’t it instead be more advantageous to instead add print where it actually matters, and have the message that’s sent to output be relevant to where the code reached? And it’s worth noting that ROBLOX does of course have a built in debugger… https://developer.roblox.com/en-us/articles/Lua-debugger

In reguards to your question, it may be of help if we had the code behind the “appear” and “fade” functions. As well as some additional context on the code’s intent.

It still did the same thing. Weird…

local function fade(map)

local interactive = map.Interactives:FindFirstChild("Interactive_"..currentbutton.Value)

interactive.Transparency = 1

interactive.CanCollide = false

end

local function appear(map)

local interactive = map.Interactives:FindFirstChild("Interactive_"..currentbutton.Value)

interactive.Transparency = 0

interactive.CanCollide = true

end

Do any of the things you are trying to find using FindFirstChild return false or nil? , it should work I believe, as long as there are no errors and the things you are trying to reference exist.

Inside of cbutton there is a value that is named, “_Fade”.

Edit: There are completely no errors.

1 Like

That’s weird, also now that I understand (after re-reading your post/code) what you are trying to do the if statements probably aren’t super important ( elseif is fine, assuming that one value is in cbutton at one time).

However Quick question, Are the fade and appear functions running at all?

Also if the “_Fade” value or whatever doesn’t exist adding some wait for childs might be to your benefit (depending wether you are fine with the function yielding)

What do you mean by ‘running’? And the _Fade value is surely inside the button.

What I am trying to say is the local function fade(map)(the function inside the spawn function) being called at all?

Yes. Let me show you:

local function fade(map)
	local interactive = map.Interactives:FindFirstChild("Interactive_"..currentbutton.Value)
	interactive.Transparency = 1
	interactive.CanCollide = false
end


local function appear(map)
	local interactive = map.Interactives:FindFirstChild("Interactive_"..currentbutton.Value)
	interactive.Transparency = 0
	interactive.CanCollide = true
end

local function unanchor(map)
	local interactive = map.Interactives:FindFirstChild("Interactive_"..currentbutton.Value)
	interactive.Anchored = false
	wait(map.Buttons:FindFirstChild("Button_"..currentbutton.Value)._Unanchor.Delay.Value)
	interactive.Transparency = 1
	interactive.CanCollide = false
end

local function check(m)
	print("tpa")
	local cbutton = m.Buttons:FindFirstChild("Button_"..currentbutton.Value)
	print("tpa")
	if cbutton:FindFirstChild("_Fade") then
		print("tpa")
		spawn(function()
		print("tpa")	
		fade(m)
		print("tpa")
		end)
	print("tpa")	
end
	if cbutton:FindFirstChild("_Appear") then
		print("tpa")
		spawn(function()
			print("tpa")
		appear(m)
		print("tpa")
		end)
		print("tpa")
   end
	if cbutton:FindFirstChild("_Unanchor") then
		print("tpa")
		spawn(function()
			print("tpa")
		appear(m)
		print("tpa")
		end)
	end
end