Calculating the distance between two gui elements, sulution from devforum did not work

I want to calculate the postion between 2 gui elemnets, but it doesn’t work.
The game wroks the following way: You click on a button and it should go to your cursor until you let go and if the gui element is close to another gui element then make an output. The buttons are inside a frame that has UIGridLayout so while I’m moving the gui part I have to place it inside a folder that is inside the frame to don’t lose its size (since its set to only scale).
Here is my code:

local function movabel(choosen, itemHolder, template, player)
	local OriginalParent = choosen.Parent
	local Holder
	local allowed = false
	local mouse = player:GetMouse()
	
	if choosen:IsA("TextButton") then
		choosen.MouseButton1Down:Connect(function()
			allowed = true
			choosen.Parent = OriginalParent.move
			while allowed do
---------------------------------------problem---------------------------------------------------------------
				TweenService:Create(choosen, TweenInfo.new(0.3, Enum.EasingStyle.Back), {Position = UDim2.new(0,mouse.X,0,mouse.Y)-OriginalParent.Position}):Play() --The OriginalParent.Postion is needed so that the textbutton lines up wtih the mouse, but it doesn't work
				if (choosen.AbsolutePosition - OriginalParent.AbsolutePosition - template.AbsolutePosition).Magnitude <= 25 then
---------------------------------------problem---------------------------------------------------------------
					if not Holder then
						Holder = placeholder:Clone()
						Holder.Parent = template
					end
				else
					if Holder then 
						Holder:Destroy()
					end
				end
				wait()
			end
		end)
		player:GetMouse().Button1Up:Connect(function()
			allowed = false
			if not Holder then
				TweenService:Create(choosen, blok_tweenInfo, {Position = OriginalParent.Position}):Play()
				wait(0.298)
				choosen.Parent = OriginalParent
			else
				choosen.Parent = Holder.Parent
				Holder:Destroy()
			end
		end)
	end
end

function stock(player)
	local PlayerGui = player:FindFirstChild("PlayerGui")
	if PlayerGui then
		local talkUI = PlayerGui:FindFirstChild("talkUI")
		if talkUI then
			repeat
				local block = TalkingModules.style[math.random(1,#TalkingModules.style)] --list the  1. is a string
				local choosen = itemHolder.style:Clone() --sample textButton
				choosen.Text = block[1]
				choosen.Parent = talkUI.Styles
				choosen.Size = choosen.Parent.UIGridLayout.CellSize --to keep size of the
				movabel(choosen,itemHolder,choosen.Parent,player)
			end
		until #choosen_style == MAX_BLOCK_LIMIT --deosn't matters
		end
	end
end

Ask any questions if this wasn’t clear.

if ((choosen.AbsolutePosition-OriginalParent.AbsolutePosition) - template.AbsolutePosition).Magnitude <= 25 then

By the way, you have an extra parenthesis before “choosen.AbsolutePosition”

Where am I missing the parenthesis?

I’m sorry, the first thing I wrote was incorrect, you actually either have a missing parenthesis or an extra one. I’m not sure because I don’t usually format magnitude checks like that.

I would just do:

if (choosen.AbsolutePosition-OriginalParent.AbsolutePosition - template.AbsolutePosition).Magnitude <= 25 then

Instead of this:

if ((choosen.AbsolutePosition-OriginalParent.AbsolutePosition) - template.AbsolutePosition).Magnitude <= 25 then

Edit: The code you have should be working just fine, maybe it’s just this error, I don’t know. If there’s something in the output that would be useful to show.

Oh yeah I wrote an extra useless one. Thanks for pointing that out for me, but unfortunately it deosn’t have anything to do with the issue.

Wait does the code even reach the Tween part? Check if it does even get there.

Top problem line has an extra close parenthesis at the end

Edit: Sorry I was originally right

Wait also you’re not subtracting the position you’re getting the negative position of it cause there isn’t a space in between I think:

Here is a video about it:

Wait why are you subtracting the Mouse’s x and y coordinates from the position of the textbutton?

TweenService:Create(choosen, TweenInfo.new(0.3, Enum.EasingStyle.Back), {Position = UDim2.new(0,mouse.X,0,mouse.Y)-OriginalParent.Position}):Play()

I’m subtracting the position of the orginal parent (textbutton.Parent.Parent)
If I would do that the button wouldn’t even apear

The effect is the same without Tweening

Hm I’m not sure. Maybe someone else might be able to assist you better. Well, good luck.

1 Like