Help with making < or > variable

So i have a line of code that is copied 4 times and the only difference is in some cases it has to check if bigger and in other cases it has to check if its smaller, and i wouldnt be able to put a variable for > or < because this:

local var = <
if 1 - 2 var 0 then

wouldnt work in any way.

my brother knew the answer, explained it to me but i couldnt understand it at all, and id like to understand then to just make it work, if anyone can take a bit of time to explain, this is the code he showed me:

local N = -1
print(N*10 > 0)

it prints False.

Thank you, have a nice day! :slight_smile:

This is my code:

local childs = game.Players.LocalPlayer.leaderstats.PlayersMet:GetChildren()
local mouse = game:GetService("UserInputService")

local function selectfunction(axis1, axis2)
	if card.ImageTransparency == 0 and script.Parent.Parent.Visible == true and loading == false then
		local obj = frame:FindFirstChild(selected)
		local closest
		for i,v in pairs(frame:GetChildren()) do
			if v.Position.axis1 == obj.Position.axis1 then
				if obj.Position.axis2.Scale - v.Position.axis2.Scale > 0 then
					if closest then
						if frame:FindFirstChild(closest).Position.axis2.Scale - v.Position.axis2.Scale < 0 then
							closest = v.Name
						end
					else
						closest = v.Name
					end
				end
			end
		end
		if closest then
			obj.BorderSizePixel = '0'
			selected = closest
			frame:FindFirstChild(closest).BorderSizePixel = '2'
		end
	end
end

local function downfunction()
	if card.ImageTransparency == 0 and script.Parent.Parent.Visible == true and loading == false then
		local obj = frame:FindFirstChild(selected)
		local closest
		for i,v in pairs(frame:GetChildren()) do
			if v.Position.X == obj.Position.X then
				if obj.Position.Y.Scale - v.Position.Y.Scale < 0 then
					if closest then
						if frame:FindFirstChild(closest).Position.Y.Scale - v.Position.Y.Scale > 0 then
							closest = v.Name
						end
					else
						closest = v.Name
					end
				end
			end
		end
		if closest then
			obj.BorderSizePixel = '0'
			selected = closest
			frame:FindFirstChild(closest).BorderSizePixel = '2'
		end
	end
end

local function leftfunction()
	if card.ImageTransparency == 0 and script.Parent.Parent.Visible == true and loading == false then
		local obj = frame:FindFirstChild(selected)
		local closest
		for i,v in pairs(frame:GetChildren()) do
			if v.Position.Y == obj.Position.Y then
				if obj.Position.X.Scale - v.Position.X.Scale > 0 then
					if closest then
						if frame:FindFirstChild(closest).Position.X.Scale - v.Position.X.Scale > 0 then
							closest = v.Name
						end
					else
						closest = v.Name
					end
				end
			end
		end
		if closest then
			obj.BorderSizePixel = '0'
			selected = closest
			frame:FindFirstChild(closest).BorderSizePixel = '2'
		end
	end
end

local function rightfunction()
	if card.ImageTransparency == 0 and script.Parent.Parent.Visible == true and loading == false then
		local obj = frame:FindFirstChild(selected)
		local closest
		for i,v in pairs(frame:GetChildren()) do
			if v.Position.Y == obj.Position.Y then
				if obj.Position.X.Scale - v.Position.X.Scale < 0 then
					if closest then
						if frame:FindFirstChild(closest).Position.X.Scale - v.Position.X.Scale < 0 then
							closest = v.Name
						end
					else
						closest = v.Name
					end
				end
			end
		end
		if closest then
			obj.BorderSizePixel = '0'
			selected = closest
			frame:FindFirstChild(closest).BorderSizePixel = '2'
		end
	end
end

local function confirmfunction()
	if card.ImageTransparency == 0 and par.Parent.Visible == true and loading == false then
		for i,v in pairs(frame:GetChildren()) do
			if v.BorderSizePixel == 2 then
				
				par.Spell:Fire(game.Players.LocalPlayer, v.Text)
				
			end
		end
	end
end

mouse.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.Up then
		selectfunction("X", "Y", )
	elseif key.KeyCode == Enum.KeyCode.Down then
		downfunction()
	elseif key.KeyCode == Enum.KeyCode.Right then
		rightfunction()
	elseif key.KeyCode == Enum.KeyCode.Left then
		leftfunction()
	elseif key.KeyCode == Enum.KeyCode.Return then
		confirmfunction()
	end
end)
1 Like

I’m confused why you want to store an operator in a variable?

This is supposed to happen. -1 * 10 is -10, and -10 is less than 0. Simple math.

By the way you can do this.

local greater_than = -10 > 0
1 Like

im not sure if you understood correctly what im trying to accomplish, i need a variable for < or > , or another way to get the same thing as that, to check if the answer is bigger or smaller than 0, and im not sure what the line my brother gave me does, so if you could explain it that could help

It’s supposed to print false. I explained the math. > is not a valid expression. Why do you want to store the operator itself as a variable?

greater_than is true if -10 is greater than 0 (which it isn’t), false otherwise (which it is false)

yes dont worry i understand that part, but the entire script works, its jsut that i have like 20 lines of code copied 4 times to just change the < and > , i wanna simplify it using a variable and only one function, am currently trying to use what my brother gave me not sure if itll work tho

There is still no reason to set a variable. It would take more time and ultimately be less efficient. Not to mention just not possible.

local x = 5
local isEqualToZero = x == 0

This previous block checks that x == 0 to zero and assigns the boolean to a variable.

would it be less efficient then having the same 20 lines 4 times with only 4 keys different? cause the way i did it in the beginning is very inefficient to me, trying to make it better and simpler

Please share the four lines you wish to simplify, because it’s more likely you need to make a new function that contains those four lines. The code is very repetitive.

i know im trying to remove the repetition and to make it into one function, heres the lines:

local function downfunction()
	if card.ImageTransparency == 0 and script.Parent.Parent.Visible == true and loading == false then
		local obj = frame:FindFirstChild(selected)
		local closest
		for i,v in pairs(frame:GetChildren()) do
			if v.Position.X == obj.Position.X then
				if obj.Position.Y.Scale - v.Position.Y.Scale < 0 then
					if closest then
						if frame:FindFirstChild(closest).Position.Y.Scale - v.Position.Y.Scale > 0 then
							closest = v.Name
						end
					else
						closest = v.Name
					end
				end
			end
		end
		if closest then
			obj.BorderSizePixel = '0'
			selected = closest
			frame:FindFirstChild(closest).BorderSizePixel = '2'
		end
	end
end

local function leftfunction()
	if card.ImageTransparency == 0 and script.Parent.Parent.Visible == true and loading == false then
		local obj = frame:FindFirstChild(selected)
		local closest
		for i,v in pairs(frame:GetChildren()) do
			if v.Position.Y == obj.Position.Y then
				if obj.Position.X.Scale - v.Position.X.Scale > 0 then
					if closest then
						if frame:FindFirstChild(closest).Position.X.Scale - v.Position.X.Scale > 0 then
							closest = v.Name
						end
					else
						closest = v.Name
					end
				end
			end
		end
		if closest then
			obj.BorderSizePixel = '0'
			selected = closest
			frame:FindFirstChild(closest).BorderSizePixel = '2'
		end
	end
end

local function rightfunction()
	if card.ImageTransparency == 0 and script.Parent.Parent.Visible == true and loading == false then
		local obj = frame:FindFirstChild(selected)
		local closest
		for i,v in pairs(frame:GetChildren()) do
			if v.Position.Y == obj.Position.Y then
				if obj.Position.X.Scale - v.Position.X.Scale < 0 then
					if closest then
						if frame:FindFirstChild(closest).Position.X.Scale - v.Position.X.Scale < 0 then
							closest = v.Name
						end
					else
						closest = v.Name
					end
				end
			end
		end
		if closest then
			obj.BorderSizePixel = '0'
			selected = closest
			frame:FindFirstChild(closest).BorderSizePixel = '2'
		end
	end
end

there was a fourth function, upfunction(), but i removed it to try and simplify it all but the code was the same as these

oh and i didnt mention it but the code checks the closest textlabel either up, right, left or down, and i dont need to fix it since it works, just to simplify things makes it alot better in my opniion

You have given us too much code here. Only show the code that is necessary.


Looks like you’re having an xy problem. You actually want to compare multiple numbers in a cleaner fashion, but you instead obscure this question with “how do i store an operator in a variable”. You should be asking only about your goal without presupposing a method that isn’t appropriate.

all the code i have given is needed for the script to function correctly, so im not sure where i have given too much code. sorry if i did tho

and my goal is to store an operator in a variable, or anything equal to that, im not sure how else to explain what im trying to achieve

It is impossible to store an operator in a variable.

This is a more complex issue and that is not a possible solution. The best route is to use a function and place your if statements in there.

Simple answer: You cannot make an operator a variable. Any operator in lua requires having a product on both sides to make a result. So, you will not be able to make var: <, >, +, -, … etc

alright thank you, i figured i couldnt but my question is, how would i make something work equally to that

and that would require having 4 different possibilites seeing as there are 4 directions correct?

also would it be possible to store X or Y in a variable? ex:

part.Position.var1

something like that

Yes, storing those values are possible.

local x = Frame.Position.X

A method you can use is a function for each operation you are attempting:

function isEqual(new, old)
    return new == old
end

function isHigherThanZero(new, old)
    return new - old > 0 
end

function isLowerThanZero(new, old)
    return new - old < 0
end

i also found out that doing

part.Position[var1]

also works correctly :smiley:

1 Like