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!
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)
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
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
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.
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
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