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

How can I properly Calculate the Position of a GUI?

Further In-Depth of the Title: How can I properly Calculate the Positioning of a GUI so that it does not end up going out of Position?

As the Title says, I am having trouble finding a Calculation to prevent my 'CmdsGui' function from going past a certain limit, however, when I attempt to, it only moves slightly, and doesn't respond anymore.

Sadly, I can not provide any Output because there is no Error present, however, I think I made Errors in my Calculations, because, as stated before, it only moves slightly, then does not respond anymore.

I have only provided a Snippet of the code due to the code being 31 pages long, and looking very, very, very messy, however, this Snippet of the code is where it is not working:

local NumberOfCommands = 136
local num2 = 0;
local UP = false;
local DOWN = false;

-- INFORMATION --
local Arrow --Arrow is the GUI that you click that moves the GUI upward
local Arrow2 --Arrow2 is the second GUI that you click that moves the GUI downward
local Frame2 --Frame2 is the Positioner for the Commands to move Up and Down

Arrow.MouseButton1Down:connect(function()
	UP = true DOWN = false
	Arrow.MouseButton1Up:connect(function() UP = false end)
	repeat wait(1/13.37)
		if Frame2.Position.Y.Scale >= 0.1 then break end
		num2 = -0.1
		Frame2:TweenPosition(UDim2.new(0,0,0.5*num2,0),"Out","Quad",1/13.37,true)
	until not UP
end)

Arrow2.MouseButton1Down:connect(function()
	UP = false DOWN = true
	Arrow2.MouseButton1Up:connect(function() DOWN = false end)
	repeat wait(1/13.37)
		if Frame2.Position.Y.Scale >= 0.3-((NumberOfCommands-1)) then break end
		num2 = 0.1
		Frame2:TweenPosition(UDim2.new(0,0,0.5*num2,0),"Out","Quad",1/13.37,true)
	until not DOWN
end)

Sorry if it sounds like I am being Picky or Rude, I have been trying to figure this out for an Hour now, but, if I need to post the entire code, just let me know.

!EDIT!

This is the Entire code, as requested;

local NumberOfCommands = 136

function CmdsGui() --I know, messy ;-;
	local num = 0; local num2 = 0; local UP = false; local DOWN = false;
	local CMDS = Instance.new("ScreenGui") CMDS.Name = "CMDS"
	local Dragger = Instance.new("TextButton",CMDS) Dragger.Name = "Drag" Dragger.Draggable = true Dragger.BackgroundTransparency = 1 Dragger.Position = UDim2.new(0.4,0,0.2,0) Dragger.Text = "" Dragger.Size = UDim2.new(0.4,0,0.15,0)
	local Frame = Instance.new("Frame",Dragger) Frame.Name = "Displayer" Frame.Size = UDim2.new(1,0,2,0) Frame.Position = UDim2.new(0,0,0.3,0) Frame.ClipsDescendants = true Frame.BackgroundColor3 = Color3.new(0/255,0/255,0/255) Frame.BorderColor3 = Color3.new(0/255,0/255,0/255)
	local Frame2 = Frame:Clone() Frame2.Name = "CmdsDisplayer" Frame2.Position = UDim2.new(0,0,0,0) Frame2.Size = UDim2.new(1,0,0.1,0) Frame2.Parent = Frame Frame2.ClipsDescendants = false
	local Cmd = Instance.new("TextLabel") Cmd.Size = UDim2.new(1,0,1,0) Cmd.TextColor3 = Color3.new(0/255,0/255,0/255) Cmd.TextStrokeColor3 = Color3.new(0/255,0/255,0/255) Cmd.TextWrapped = true Cmd.TextScaled = false Cmd.Name = "Cmd" Cmd.Text = "" Cmd.TextColor3 = Color3.new(255/255,255/255,255/255) Cmd.TextStrokeColor3 = Color3.new(255/255,255/255,255/255) Cmd.BorderColor3 = Color3.new(0/255,0/255,0/255) Cmd.BackgroundColor3 = Color3.new(0/255,0/255,0/255) Cmd.TextStrokeTransparency = 0.8
	local Arrow = Instance.new("TextButton",Dragger) Arrow.Text = "/\\" Arrow.Name = "UP" Arrow.Position = UDim2.new(-0.1,0,0.3,0) Arrow.Size = UDim2.new(0.1,0,0.5,0) Arrow.TextWrapped = true Arrow.TextScaled = false Arrow.TextColor3 = Color3.new(255/255,255/255,255/255) Arrow.TextStrokeColor3 = Color3.new(255/255,255/255,255/255) Arrow.BackgroundColor3 = Color3.new(0/255,0/255,0/255) Arrow.BorderColor3 = Color3.new(0/255,0/255,0/255) Arrow.TextStrokeTransparency = 0.8
	local Arrow2 = Arrow:Clone() Arrow2.Text = "\\/" Arrow2.Name = "DOWN" Arrow2.Position = UDim2.new(-0.1,0,0.8,0) Arrow2.Parent = Dragger
	local Exit = Arrow:Clone() Exit.Text = "X" Exit.Name = "EXIT" Exit.Position = UDim2.new(-0.1,0,-0.2,0) Exit.Parent = Dragger
	
	Arrow.MouseButton1Down:connect(function()
		UP = true DOWN = false
		Arrow.MouseButton1Up:connect(function() UP = false end)
		repeat wait(1/13.37)
			if Frame2.Position.Y.Scale >= 0.1 then break end
			num2 = -0.1
			Frame2:TweenPosition(UDim2.new(0,0,0.5*num2,0),"Out","Quad",1/13.37,true)
		until not UP
	end)
	
	Arrow2.MouseButton1Down:connect(function()
		UP = false DOWN = true
		Arrow2.MouseButton1Up:connect(function() DOWN = false end)
		repeat wait(1/13.37)
			if Frame2.Position.Y.Scale >= 0.3-((NumberOfCommands-1)) then break end
			num2 = 0.1
			Frame2:TweenPosition(UDim2.new(0,0,0.5*num2,0),"Out","Quad",1/13.37,true)
		until not DOWN
	end)
	return CMDS,Frame2,Cmd,num
end

1 answer

I have been working on a GUI moving script all you would have to do is validate the new position of the GUI.

This is a local script, go to my place as I currently have it uploaded so you can see it working. Hope this helps you

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local mouseClick = false
local curX
local curY
local startPosition


Mouse.Move:connect(function()
	if mouseClick then
		--Calculate new Udim2 position and then validate it here 

		--Move gui
		script.Parent:TweenPosition(startPosition + UDim2.new(0,Mouse.X - curX,0,Mouse.Y - curY),nil,nil,0.002)
	end
end)

script.Parent.InputEnded:connect(function (inp)
	if inp.UserInputType.Value == 0 then
		mouseClick = false
	end
end)
script.Parent.InputBegan:connect(function (inp)
	if inp.UserInputType.Value == 0 then
		curX = Mouse.X
		curY = Mouse.Y
		startPosition = script.Parent.Position
		mouseClick = true
		
	end
end)

Edit:- I was going to add parameters to stop this GUI from going outside the screen so if you are doing this save me some time and post me the script.

Update:-

I did not manage to get it working 100% :(

The problem is that if position of the mouse is outside the main frame then the events do not get called so that the dragging does not stop.

Currently I limited this frame to -300 x offset, Just paste this script into a new project.

local NumberOfCommands = 136

function CmdsGui() --I know, messy ;-;
    local num = 0
	local num2 = 0
	local Player = game.Players.LocalPlayer
	local Mouse = Player:GetMouse()
	
    local CMDS = Instance.new("ScreenGui") 
	CMDS.Name = "CMDS"
	
    local Dragger = Instance.new("TextButton",CMDS)
	Dragger.Name = "Drag" 
	Dragger.Draggable = true 
	Dragger.BackgroundTransparency = 1 
	Dragger.Position = UDim2.new(0.4,0,0.2,0) 
	Dragger.Text = "" 
	Dragger.Size = UDim2.new(0.4,0,0.15,0)
	
    local Frame = Instance.new("Frame",Dragger) 
	Frame.Name = "Displayer" 
	Frame.Size = UDim2.new(1,0,2,0) 
	Frame.Position = UDim2.new(0,0,0.3,0) 
	Frame.ClipsDescendants = true 
	Frame.BackgroundColor3 = Color3.new(0/255,0/255,0/255) 
	Frame.BorderColor3 = Color3.new(0/255,0/255,0/255)
	
    local Frame2 = Frame:Clone() 
	Frame2.Name = "CmdsDisplayer" 
	Frame2.Position = UDim2.new(0,0,0,0) 
	Frame2.Size = UDim2.new(1,0,0.1,0) 
	Frame2.Parent = Frame 
	Frame2.ClipsDescendants = false
	
    local Cmd = Instance.new("TextLabel") 
	Cmd.Size = UDim2.new(1,0,1,0) 
	Cmd.TextColor3 = Color3.new(0/255,0/255,0/255) 
	Cmd.TextStrokeColor3 = Color3.new(0/255,0/255,0/255) 
	Cmd.TextWrapped = true 
	Cmd.TextScaled = false 
	Cmd.Name = "Cmd" 
	Cmd.Text = "" 
	Cmd.TextColor3 = Color3.new(255/255,255/255,255/255) 
	Cmd.TextStrokeColor3 = Color3.new(255/255,255/255,255/255) 
	Cmd.BorderColor3 = Color3.new(0/255,0/255,0/255) 
	Cmd.BackgroundColor3 = Color3.new(0/255,0/255,0/255) 
	Cmd.TextStrokeTransparency = 0.8
	
    local Arrow = Instance.new("TextButton",Dragger) 
	Arrow.Text = "/\\" 
	Arrow.Name = "UP" 
	Arrow.Position = UDim2.new(-0.1,0,0.3,0) 
	Arrow.Size = UDim2.new(0.1,0,0.5,0) 
	Arrow.TextWrapped = true 
	Arrow.TextScaled = false 
	Arrow.TextColor3 = Color3.new(255/255,255/255,255/255) 
	Arrow.TextStrokeColor3 = Color3.new(255/255,255/255,255/255) 
	Arrow.BackgroundColor3 = Color3.new(0/255,0/255,0/255) 
	Arrow.BorderColor3 = Color3.new(0/255,0/255,0/255) 
	Arrow.TextStrokeTransparency = 0.8
	
    local Arrow2 = Arrow:Clone() 
	Arrow2.Text = "\\/" 
	Arrow2.Name = "DOWN" 
	Arrow2.Position = UDim2.new(-0.1,0,0.8,0) 
	Arrow2.Parent = Dragger
	
    local Exit = Arrow:Clone() 
	Exit.Text = "X" 
	Exit.Name = "EXIT" 
	Exit.Position = UDim2.new(-0.1,0,-0.2,0) 
	Exit.Parent = Dragger
	
	local dragging  =  false	

	
	Dragger.MouseMoved:connect(function(ud)
		dragging = true
		print("Drag")
		local curPos = Dragger.Position
		print(curPos.X.Offset)
		repeat wait()
			if curPos.X.Offset < -300 then
				
				 Dragger.Position = UDim2.new(0.4, -299 ,0.15, curPos.Y.Offset)
				 dragging  =  false	
			end
		until not dragging
		
	end)
	Dragger.DragStopped:connect(function (x, y)
		dragging  =  false	
		print("Stop")
	end)

    Arrow.MouseButton1Down:connect(function()
		
    end)

    Arrow2.MouseButton1Down:connect(function()

    end)
    return CMDS,Frame2,Cmd,num
end

game.Players.PlayerAdded:connect(function (plr)
	print("ok")
	local plrGui = plr:WaitForChild("PlayerGui")
	print("ok")
	local gui = CmdsGui()
	print("ok")
	gui.Parent = plrGui
end)

4 comments

0

Are you sure you want me to post the entire code? I will, however, I have to warn you, it looks Super messy.

0

You can post the code or upload the roblox studio project and I can have a look at it

0

Alright, I have posted the entire code.

0

Sorry but I could not fix it 100%

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