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
			allowed = true
			choosen.Parent = OriginalParent.move
			while allowed do
				TweenService:Create(choosen,, Enum.EasingStyle.Back), {Position =,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
					if not Holder then
						Holder = placeholder:Clone()
						Holder.Parent = template
					if Holder then 
			allowed = false
			if not Holder then
				TweenService:Create(choosen, blok_tweenInfo, {Position = OriginalParent.Position}):Play()
				choosen.Parent = OriginalParent
				choosen.Parent = Holder.Parent

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

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,, Enum.EasingStyle.Back), {Position =,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