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

How do I embed a function inside a function?

I was just having a look around in studio and keep on recognizing that you cannot embed a function inside a function just by simply doing:

script.Parent.MouseButton1Click:connect(function()
print("Button Has Been Pressed")
script.Parent.Value.Changed:connect(function()
print("Value Has Changed")
end)
end)

or by doing this:

script.Parent.MouseButton1Click:connect(function()

function OnValueChanged()
print("Value Has Changed")
script.Parent.Value.Changed:connect(OnValueChanged()

end)

How would I embed a function into another function, especially with values requiring a "changed" event to sense that the value has changed.

Thanks.

2 comments

0

Line 5 on the second block should be: "script.Parent.Value.Changed:connect(OnValueChanged)"

0

On your first code, line 3, what is 'script.Parent.Value'?

3 answers

✓ Accepted answer

If using an embed technique does not work, you can try spawn():

function FuncOne()
	spawn(FuncTwo)
	script.Parent.Transparency = 0
end
function FuncTwo()
	wait(1)
	script.parent.Transparency = 1
end

Please note that the spawned function has to have a wait() in it.

1 comment

0

Thanks! I will also have a look at this method.

Hi Nikkasfied. The answer below/above me works but that is not the only way to do so. There are numerous ways you can do it and I will show you 3 examples of the ones that work

To show you an example, I have 2 Bricks in Workspace, named ClickBrick and ChangeColour. When clicked on the ClickBrick it will result in printing, "Brick is Clicked". Then when clicked on the ChangeColour Brick, the brick will change into red colour.

  1. First One
-- Declaration Section 
--//Game Services 
local Workspace = game:GetService("Workspace")

--//Assets 
local button = Workspace:WaitForChild("ClickBrick")
local ColourChanger = Workspace:WaitForChild("ChangeColour")

-- Processing Section
local function printButtonWhenClicked ()
	print("It is clicked!")
	ColourChanger.ClickDetector.MouseClick:Connect(function()
		ColourChanger.BrickColor = BrickColor.Red()
		print("Changed to red colour!")
	end)
end

button.ClickDetector.MouseClick:Connect(printButtonWhenClicked)
  1. Second One
-- Declaration Section 
--//Game Services 
local Workspace = game:GetService("Workspace")

--//Assets 
local button = Workspace:WaitForChild("ClickBrick")
local ColourChanger = Workspace:WaitForChild("ChangeColour")

-- Processing Section
button.ClickDetector.MouseClick:Connect(function()
	print("It is clicked!")
	ColourChanger.ClickDetector.MouseClick:Connect(function()
		ColourChanger.BrickColor = BrickColor.Red()
		print("Changed to red colour!")
	end)
end)
  1. Third One
-- Declaration Section 
--//Game Services 
local Workspace = game:GetService("Workspace")

--//Assets 
local button = Workspace:WaitForChild("ClickBrick")
local ColourChanger = Workspace:WaitForChild("ChangeColour")

-- Processing Section

changePartColour = function()
	print("It is clicked!")
	local function changeBrickColour ()
		ColourChanger.BrickColor = BrickColor.Red()
		print("Changed to red colour!")
	end
	ColourChanger.ClickDetector.MouseClick:Connect(changeBrickColour)
end

button.ClickDetector.MouseClick:Connect(changePartColour)

These are the only three examples I will show you. There are Methods and Tables too which you can apply similar concept.

I recommend checking out more on the wiki for information

####Anonymous Function ####Global Function ####Functions

Some other good posts in ScriptingHelpers as well

#####What are the Differences between "Local Function" and "Function"?

Have a lovely day of coding

1 comment

0

Thanks for the help! I will try these methods out.

You'll want to have this.

script.Parent.MouseButton1Click:connect(function()


script.Parent.Value.Changed:connect(function()
--Your code here
end)
end)

Hope this helps!

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