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

Why cant I detect Enums with "if" statements?

function FaceToAxis(face)
	if face == Enum.NormalId.Front then
		local axis = "Z"
		local direction = 1
	elseif face == Enum.NormalId.Back then
		local axis = "Z"
		local direction = -1
	elseif face == Enum.NormalId.Left then
		local axis = "X"
		local direction = 1
	elseif face == Enum.NormalId.Right then
		local axis = "X"
		local direction = -1
	elseif face == Enum.NormalId.Top then
		local axis = "Y"
		local direction = 1
	elseif face == Enum.NormalId.Bottom then
		local axis = "Y"
		local direction = -1
	end
	return axis, direction
end

It always returns nil and nil. I'm calling the function with the Face value of a SurfaceGui. If I print face then it tells me it is infact Enum.NormalId.Front.

Whats wrong here?

1 answer

You need to make the axis and direction variables accessible outside of the if statements. This could easily be fixed by defining them at the top of the function.

function FaceToAxis(face)
    local axis
    local direction
    if face == Enum.NormalId.Front then
        axis = "Z"
        direction = 1
	elseif face == Enum.NormalId.Back then
        axis = "Z"
        direction = -1
    elseif face == Enum.NormalId.Left then
        axis = "X"
        direction = 1
    elseif face == Enum.NormalId.Right then
        axis = "X"
        direction = -1
    elseif face == Enum.NormalId.Top then
        axis = "Y"
        direction = 1
    elseif face == Enum.NormalId.Bottom then
        axis = "Y"
        direction = -1
    end
    return axis, direction
end

3 comments

0

I already have done that before any other code in my script, the problem is that it isnt detecting when face is one of the options.

0

That piece of code works perfectly on my end.

0

It detects if face equals one of the options?

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