If and else seem to be firing at the same time

Im trying to make this condition work but they both fire for some reason and I cant figure out why

local equation = (itemLocation - slotLocation).Magnitude < 95
if equation then
	print(item.Name.." is close to Slot "..slot.Name)
	item.Parent = slot
	item.Position = slot.Center.Position
else
	print("Not close")
end

1 Like

have you tried using elseif?
Example:

local equation = (itemLocation - slotLocation).Magnitude <95
if equation then
print (item.Name.."is close to Slot"..slot.Name)
item.Parent = slot
item.Position = slot.Center.Position
elseif not equation then
print ("Not close")
end
1 Like

Maybe try this:

if (itemLocation - slotLocation).Magnitude < 95 then
    -- code
else
    -- code
end

I tried that and its still printing both of them in the output sadly

Alright let me try that quickly

edit : i didnt work either not surprisingly @P1X3L_K1NG

Do you mind sharing the code bit that initializes itemLocation and slotLocation please?

1 Like

Is equation wrapped in a loop? I don’t think your if and else statements are firing at the same time, but instead the equation is still being checked after the position of item is changed. That could explain why it is still printing “not close” after you place the item down.

Has this been ruled out yet?

1 Like

edit: that formatting lol

local function findNearestFrame()
			for _, slot in pairs(List:GetChildren()) do
				for number = 1,maxSlots,1 do
					if slot.Name == tostring(number) then
						for _, item in pairs(List:GetDescendants()) do
							if item:IsA("ImageLabel") and item.Name ~= "Item" then
								local itemLocation = Vector2.new(item.AbsolutePosition.X,item.AbsolutePosition.Y) + item.AbsolutePosition / 2
								local slotLocation = Vector2.new(slot.AbsolutePosition.X,slot.AbsolutePosition.Y) + slot.AbsolutePosition / 2
								local equation = (itemLocation - slotLocation).Magnitude < 95
								if (itemLocation - slotLocation).Magnitude < 95 then
									print(item.Name.." is close to Slot "..slot.Name)
									item.Parent = slot
									item.Position = slot.Center.Position
								else
									print("Not close")
                                    break -- added this, see if that fixes the problem
								end
							end
						end
					end
				end
			end
		end
1 Like

Alright I tried to put the break and it highlights print
image

Put the break after print, forgot it wont run past that portion.

Alright I just did that and it still is printing both print statements im stumped at this point
image

I’m still not convinced they’re both firing at the same time. Can you provide the code that fires findNearestFrame()

1 Like

Its a input ended function from userinputservice

Can you still paste it? Your problem is most likely coming from how frequently you’re firing findNearestFrame()

Any ideas??? @TheeDeathCaster @v7zr

I don’t think anyone will be able to provide you with any more information until you provide the code that handles the input.

That has nothing to do with the problem, it has to be something with the loop