If and else executing simultaneously?!

Im check if two Vectors are within a certain range of each other and if it is then itll do something but if it isnt itll do something else, problem is they both fire at the same time for unknown reasons

local itemLocation = Vector2.new(item.AbsolutePosition.X,item.AbsolutePosition.Y)
local slotLocation = Vector2.new(slot.AbsolutePosition.X,slot.AbsolutePosition.Y)
local equation = (itemLocation - slotLocation).Magnitude < 95
if equation then
	item.Parent = slot
	item.Position = slot.Center.Position
	print("Slotted into "..slot.Name.."!")
else
	print("NOT CLOSE")
end

What you described happening is impossible as the way the way that if and else statements work is if the condition is not met then it falls onto the else

Also its not that its running both of them that specific statement of code is running once for every single slot to figure out what slot it should enter. (Or thats what I assume from looking at it)

1 Like

Alright so what should I do about that then?

Nothing the script is working perfectly fine although if its annoying you, you can delete the print statements I guess

Yeah I could get rid of the prints but its not just that but I dont plan on just printing stuff out in the else, I want it to actually do stuff but if there both firing near the same time I cant

What do you want to do? (charrrr)

I basically want to put the item back in the slot it was in if it isnt close to any of the other slots

edit : Like basically say its far out of the range of any slots itll go back to the one it was at originally

problem is thats not the hard part

You wouldn’t put that code in the if and else you would put that after that Also the code is in some sort of loop so you would have to put it outside of the loop

What I would do is do some sort of bool value to determine if it gets put in a slot and if that value equals false then return it to where it was before

But I would put it in there though, I need to check if it isnt in the range of any of the slots

Nope because that code that you have is running mutible times to check every single slot so if you put it in there it would return to its previous slot every single time because most of the time its gonna go through its gonna be in the else section as it is far away from all of the slots.

Im really stumped right now how would I fix the code though

The code works perfectly fine that is not the issue you just need to add something later on in the code that returns it to its previous slot if it did make it into a slot

Alright but the loop has all the info I need though how will I retrieve the item and or the slot

just add a normal variable to it

 local itemLocation = Vector2.new(item.AbsolutePosition.X,item.AbsolutePosition.Y)
local slotLocation = Vector2.new(slot.AbsolutePosition.X,slot.AbsolutePosition.Y)
local equation = (itemLocation - slotLocation).Magnitude < 95
if equation then
	item.Parent = slot
	item.Position = slot.Center.Position
	print("Slotted into "..slot.Name.."!")
    Slotted = true
else
	print("NOT CLOSE")
end

Just return it if it equals true then make sure that each time you run it before the loop you make sure that Slotted = false

1 Like

OH ALRIGHT, its WORKING ACTUALLY good wait hold on
@poopnugget142 THANKS you actually saved me man ive been stuck on this for so long thanks for taking the time to help me man