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

Function within a function.?

function t()
	body.Position = Vector3.new(-229, -34.8, 41.235)
	function t()
		body.Position = Vector3.new(-220, -34.8, 41.235)
		
		function t()
			body.Position = Vector3.new(-114, -34.8, 41.235)
		end
	end
end
script.Parent.Parent.touch.Touched:connect(t)
function d()
end
function y()
end

Okay so I'm making a script, if a brick gets touched three times in row it will(pick one): change color ,position,name,etc.

The concept of the script is that if the certain brick gets touched three times in a row, the position of the brick will move. if gets touched three more times, the position will change again.

This is what I have

2 answers

✓ Accepted answer

Nesting functions aren't the solution to your problem. Instead, your function should be based off a variable that changes upon the conditions of the function. Like this:

local State = 0

script.Parent.Parent.touch.Touched:connect(function(Part)
	State = State + 1 -- Add 1
	if State == 3 then -- When touched 3 times, it will move and start over.
		State = 0
		Part.CFrame = CFrame.new(0,0,0) -- new location
	end
end)

Hope this helped, let me know if you have any questions.

1 comment

0

Hey! Wow Man. I've never thought of it that way but i have a feeling this would work. Thank you so much

You not only have nested functions but you also have functions of the same name, what will happen with this is that you will have all three functions called all at ounce. What you can do instead is use some if statements.

bob=0
bounce=true
function t()
	if bounce=true then --[[bounce to make sure the Touched function only happens once. Add a wait after this if statement to make it disabled longer]]
		bounce=false
		bob=bob+1
		if bob==1 then
			body.Position = Vector3.new(-229, -34.8, 41.235)
		elseif bob==2 then
			body.Position = Vector3.new(-220, -34.8, 41.235)
		elseif bob==3 then
			bob=0 --[[Resets on the third hit, remove this to make sure it doesn't reset]]
			body.Position = Vector3.new(-114, -34.8, 41.235)
		end
		bounce=true
	end
end

script.Parent.Parent.touch.Touched:connect(t)

2 comments

0

Thank You Thank You!

0

No problem, if you found your answer please except it so other people can know that it has been answered.

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