Why can't I return this function

I am currently trying to restrict people from going below a certain angle doing a certain action.

Here’s a snippet code that is active when doing the function.

local maxAngle = math.acos(math.pi/2)
function Add(a, b, c, d)
...
if leftDot >= maxAngle and rightDot >= maxAngle then
	return function()
		adjust(wall, leftWall)
		adjust(rightWall, wall)
		walls[wall] = true
	end
end

local function rel()
...
local placeA = Add(a,b,c,d)
local placeB = Add(a,c,b,d)

if placeA and placeB then
--Code
else
end
end

However placeA and placeB always returned nil

You’ve defined them as Add, however you’re using them as add.

Functions are case sensitive, like other things.

I apologise but that is simply a typo. The actual function names are other things, I was just simplifying the names.

Also i think your missing an end?

To close off your first function

Scratch that. It’s point formed. Derp.

Can i see the whole code?

It’s hard to tell since youre not providing a b c or d

2 Likes

You are calling your function incorrectly( i believe)

I think you should call it like this:


function test()

	return  function()
		print(1)
	end

	
end


local a  = test()---a is being set to the returned function 

a()---The function is called here

It’s hard to see the issue, since you’re looking for both parameters to be true, but we can’t see where things are executing so we can’t help beyond that.

A, B, C, D is not correlated with the code that is active in the bug.

But nontheless, if it helps

local thresh = math.acos(math.pi/2)
local function addToPoll(polls, poll, otherPoll, model)
	local walls = getWalls(polls, poll)
	local dir = (poll - otherPoll).Unit
	local rightDir = dir:Cross(UP)
	
	local right, left = model.Wall1, model.Wall2
	local toRight = right.Position - left.Position
	if toRight:Dot(rightDir) < 0 then
		right, left = left, right
	end

	local wall = {
		length = {
			left = 0;
			right = 0;
		};
		model = model;
		right = right;
		left = left;
		dir = dir;
		rightDir = rightDir;
	}
	
	if not next(walls) then
		walls[wall] = true
		return
	end

	local leftIsFound
	local leftDot
	local leftWall
	
	local rightIsFound
	local rightDot
	local rightWall
	
	for otherWall in next, walls do
		local dot = dir:Dot(otherWall.dir)
		local isRight = rightDir:Dot(otherWall.dir) < 0
		if isRight then
			if not rightIsFound or not rightDot or dot > rightDot then
				rightIsFound = true
				rightDot = dot
				rightWall = otherWall
			end
			if not leftIsFound and (not leftDot or dot < leftDot) then
				leftDot = dot
				leftWall = otherWall
			end
		else
			if not rightIsFound and (not rightDot or dot < rightDot) then
				rightDot = dot
				rightWall = otherWall
			end
			if not leftIsFound or not leftDot or dot > leftDot then
				leftIsFound = true
				leftDot = dot
				leftWall = otherWall
			end
		end
	end	
	if leftDot >= thresh and rightDot >= thresh then
		return function()
			adjust(wall, leftWall)
			adjust(rightWall, wall)
			walls[wall] = true
		end
	end
	
end

local function placeWall()
	if mouseDown then
	templatePole.Transparency = 0.65
	local wall = wallModel
	wall.Parent = workspace.Walls
	wall.Wall1.Transparency = 0
	wall.Wall2.Transparency = 0
	local placeA = addToPoll(polls, pollA, pollB, wall)
	local placeB = addToPoll(polls, pollB, pollA, wall)
		if placeA and placeB then
			placeA()
			placeB()
			endWallPlacement()
		else
			print("destroy")
			wall:Destroy()
			endWallPlacement()
		end
	mouseDown = false
	end
end

isn’t the reason that place a and b are nil is because they are not actually calling the returned function? it seems like you aren’t calling the function that you returned

Try this:

If… Then
adjust(wall, leftWall)
adjust(rightWall, wall)
walls[wall] = true
Return true
End

I have tried that approach before, won’t work.

When calling the function, it should get the returned function too.

Have you tried returning true after wall[wall]=true?

Yeah, I have tried that approach before.

What does left dot and right dot return?

It should return
adjust(wall, leftWall)
adjust(rightWall, wall)
walls[wall] = true

Sorry, i meant you have local placeA =

What does that return?
Print (placeA)

Maybe this will help:

function Add(a, b, c, d)
if leftDot >= maxAngle and rightDot >= maxAngle then
	
	local func = function()---Defined here
		adjust(wall, leftWall)
		adjust(rightWall, wall)
		walls[wall] = true
	end
	
	
	return func()----Its being called/returned
	
	end
end
1 Like

No :frowning: this doesn’t work either. I have however come to the conclusion that the wall has nothing to compare its angle with initially (or rather in the beginning) so it automatically turns nil. I have an awesome human being looking into it right now though. I will come back with an answer!

1 Like

Might be due to that part? Like you’re returning nil in the “addToPolls” function before getting to the return segment for the function. It could be that it returns that before getting to it.

1 Like

This actually did make me able to placeTheWall, but it would bug after calling the function again.