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

How can I check if 2 GUIs' positions are basically the same?

if GUI1.Position == GUI2.Position then
	print("Position = Same")
end

But the above code doesn't run, because their position have that 0.000001 difference, how can I make it detect if their position is pretty much the same?

Current Script (Need Fix):

runService.RenderStepped:Connect(function()
	if (math.abs(apple.Position.X.Scale) - math.abs(snake.Position.X.Scale) <= 0.0000001) and (math.abs(apple.Position.Y.Scale) - math.abs(snake.Position.Y.Scale) <= 0.0000001) then
		--code
	end
end)

1 answer

✓ Accepted answer
local a = 1;
local b = 1.000001;

local maximumDifference = 0.01 -- Maximum difference between each value

if a == b or (math.abs(a-b) <= maximumDifference) then
	-- Code
end

for UDim2 it'd be something like this (bad code for example purposes)

local a = UDim2.new(0, 100, 0, 99.99);
local b = UDim2.new(0, 99.99, 0, 100);

local maximumOffsetDifference = 0.01 -- Maximum difference between each value
local maximumScaleDifference = 0.005 -- Maximum difference between each value

if a.X.Offset == b.X.Offset or (math.abs(a.X.Offset-b.X.Offset) <= maximumOffsetDifference) or
	a.Y.Offset == b.Y.Offset or (math.abs(a.Y.Offset-b.Y.Offset) <= maximumOffsetDifference) or
	
	a.X.Scale == b.X.Scale or (math.abs(a.X.Scale-b.X.Scale) <= maximumScaleDifference) or
	a.Y.Scale == b.Y.Scale or (math.abs(a.Y.Scale-b.Y.Scale) <= maximumScaleDifference) then
	
	-- Code
end

1 comment

0

oh wait it's a udim2 value sorry I forgot that was about gui, but it's basically the same concept, tell me if you need help with udim2

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