Weird Ui bug or code bug

So I’m making a player be able to control this ui square and it works fine but a problem i encountered is when trying to stop the player from leaving another frame. It works by updating the udim2 in a render stepped and clamping it to -1 , -1 on y . but for some reason only works on top left and middle left maybe I missing something help is appreciated.

also I tried something with anchor point but it only helps one direction

The position of gui elements have their position focused in the top left. The size then expands the box to the right and down. The box is the clip you have isnt technically leaving since the top left corner is still there, but the visual part made by the size expanding is. You’ll want to add the size to the position when checking the bounds to keep it there.

how exactly also could i adjust the anchor point to 0.5 and still do the same right.?

if you tried moving your frame to the left you will notice that its X scale isn’t exactly -1
the Position of your frame is the position of the anchor point its 0,0 by default that is why it works on the top and left only

what you can do to fix is is to replace -1,-1 with → 1 - frame.SIze.X, 1 - frame.Size.Y

here is an example script that works

script
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local frame = script.Parent -- frame to move

-- speed is how much will the frame move in X and Y 
local speed = {
	X = 0,
	Y = 0,
}


RunService.RenderStepped:Connect(function()
	local x = math.clamp(frame.Position.X.Scale + speed.X, 0, 1 - frame.Size.X.Scale) -- here
	local y = math.clamp(frame.Position.Y.Scale + speed.Y, 0, 1 - frame.Size.Y.Scale) -- and here
	frame.Position = UDim2.fromScale(x, y)
end)

--/// nvm this
UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		speed.Y = -0.01
		speed.X = 0
	elseif input.KeyCode == Enum.KeyCode.S then
		speed.Y = 0.01
		speed.X = 0
	elseif input.KeyCode == Enum.KeyCode.D then
		speed.X = 0.01
		speed.Y = 0
	elseif input.KeyCode == Enum.KeyCode.A  then
		speed.X = -0.01
		speed.Y = 0
	end
end)

Yeah you can adjust the anchor point to the center (0.5, 0.5) then do the math checks to keep in bound, by default the anchor point is in the top left.

Depending if you are using the %size or pixel size the math changes. Do note that I am not good at explaining things and do not mess with guis often

take for the pixel size you would get the size of the parent frame ( lets say 100 pixels for this ) and the child frame ( lets say 20 ) the anchor point is at (0.5, 0.5). You would take the %position and then get the child size/2 ( 20/2 = 10 ) then the child size / parent size ( 10 / 100 = .1 ) meaning that the limit is (0.9, 0.9) for the position

the %size would be similar to the example above but divide the %size/2 and have that in place for the respective scale. IE .2 size in a 100 pixel frame sets its bound to 0.9, 0.9 to keep it there

1 Like
	local moveX = 0
	local moveY = 0

	if UserInputService:IsKeyDown(Enum.KeyCode.W) then
		moveY = moveY - 0.1 * 2 * deltaTime
	end
	if UserInputService:IsKeyDown(Enum.KeyCode.S) then
		moveY = moveY + 0.1 * 2 * deltaTime
	end
	if UserInputService:IsKeyDown(Enum.KeyCode.A) then
		moveX = moveX - 0.1 * 2 * deltaTime
	end
	if UserInputService:IsKeyDown(Enum.KeyCode.D) then
		moveX = moveX + 0.1 * 2 * deltaTime
	end

	-- Calculate new position
	local x = math.clamp(playerMover.Position.X.Scale + moveX, 0, 1 - playerMover.Size.X.Scale) -- here
	local y = math.clamp(playerMover.Position.Y.Scale + moveY, 0, 1 - playerMover.Size.Y.Scale) -- and here

	
	playerMover.Position = UDim2.fromScale(x, y)


end)

this is my implementation of it but still.

Got it working with the help of both of u here’s my rendition
local limitX = 1 - (childSizeX / parentSizeX) / 2
local limitY = 1 - (childSizeY / parentSizeY) / 2
and some other calculation by realLife Lobster

1 Like

Can’t mark both of u so I don’t really know what to do

mark him because he helped first or make a reply with a link to both answers and mark it as answer

> Blockquote

> Blockquote

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.