How to check if an image is crossing outside the frame

right now im making a 2d game where the player can do stuff and so on, only problem is how do i check if an image crosses the border of the frame, and better yet how can i make it so that the image cant go outside the frame

local userinput = game:GetService("UserInputService")

while task.wait() do
	if userinput:IsKeyDown(Enum.KeyCode.W) or userinput:IsKeyDown(Enum.KeyCode.Up) then
		script.Parent.Position = script.Parent.Position + UDim2.new(0,0,-0.008,0)
	end
	if userinput:IsKeyDown(Enum.KeyCode.S) or userinput:IsKeyDown(Enum.KeyCode.Down) then
		script.Parent.Position = script.Parent.Position + UDim2.new(0,0,0.008,0)
	end
	if userinput:IsKeyDown(Enum.KeyCode.D) or userinput:IsKeyDown(Enum.KeyCode.Right) then
		script.Parent.Position = script.Parent.Position + UDim2.new(0.008,0,0,0)
	end
	if userinput:IsKeyDown(Enum.KeyCode.A) or userinput:IsKeyDown(Enum.KeyCode.Left) then
		script.Parent.Position = script.Parent.Position + UDim2.new(-0.008,0,0,0)
	end
end

also quick note i have been trying for an hour : (

ima wait for like the next hour for a responce

This function can help to check if a frame is in bounds of another frame (These don’t have to be frames; they could be any GUI element)

local function isOutOfBounds(ContainerFrame:Frame, OtherFrame:Frame) 
	local OtherPos, OtherSize = OtherFrame.AbsolutePosition,OtherFrame.AbsoluteSize 
	local ContainerPos, ContainerSize  = ContainerFrame.AbsolutePosition, ContainerFrame.AbsoluteSize

	if OtherPos.X < ContainerPos.X or
		OtherPos.Y < ContainerPos.Y or
		OtherPos.X + OtherSize.X > ContainerPos.X + ContainerSize.X or
		OtherPos.Y + OtherSize.Y > ContainerPos.Y + ContainerSize.Y then
		return true
	end
	return false
end

As for making it not go past the boundaries, I’m not super sure but you could use the function to check if the next position would make it out of bounds and then just not move the frame at all. For example, if we wanted to move to the right, but the function tells us the next position would be outside the parent frame’s bounds then we don’t move at all

thanks but can you explain how your function works?

i reworked the script is it something like this? but it doesnt work : (

local userinput = game:GetService("UserInputService")
local function isOutOfBounds(ContainerFrame:Frame, OtherFrame:Frame) 
	local OtherPos, OtherSize = OtherFrame.AbsolutePosition,OtherFrame.AbsoluteSize 
	local ContainerPos, ContainerSize  = ContainerFrame.AbsolutePosition, ContainerFrame.AbsoluteSize

	if OtherPos.X < ContainerPos.X or
		OtherPos.Y < ContainerPos.Y or
		OtherPos.X + OtherSize.X > ContainerPos.X + ContainerSize.X or
		OtherPos.Y + OtherSize.Y > ContainerPos.Y + ContainerSize.Y then
		return true
	end
	return false
end

while task.wait() do
	local check = isOutOfBounds(script.Parent,script.Parent.Parent)
	print(check)
	if userinput:IsKeyDown(Enum.KeyCode.W) or userinput:IsKeyDown(Enum.KeyCode.Up) then
		if check then
			script.Parent.Position = script.Parent.Position + UDim2.new(0,0,-0.008,0)
		end
	end
	if userinput:IsKeyDown(Enum.KeyCode.S) or userinput:IsKeyDown(Enum.KeyCode.Down) then
		if check then
			script.Parent.Position = script.Parent.Position + UDim2.new(0,0,0.008,0)
		end
	end
	if userinput:IsKeyDown(Enum.KeyCode.D) or userinput:IsKeyDown(Enum.KeyCode.Right) then
		if check then
			script.Parent.Position = script.Parent.Position + UDim2.new(0.008,0,0,0)
		end
	end
	if userinput:IsKeyDown(Enum.KeyCode.A) or userinput:IsKeyDown(Enum.KeyCode.Left) then
		if check then
			script.Parent.Position = script.Parent.Position + UDim2.new(-0.008,0,0,0)
		end
	end
end

How big is the image just set the size etc on scale then we just check if the position is > < the position within the frame by checking for -1 and then set it to -1 if it’s < -1

the size of the image (border) is {0, 375},{0, 339}

the player size is {0, 22},{0, 22}, but how do i do what your saying

Just check what’s the position if the image is outside the border and if it’s >< the border reset the position to the max Position use scale for it

is it something like this?

if script.Parent.Position > script.Parent.Parent.Position then
	---stop?
end

Ye and if it’s > position set the position like now X to what the max so it basically goes back if it’s out of the position

Check the positions for -x X -y y whats the max

i did what you said and heres the script

local userinput = game:GetService("UserInputService")
local function isOutOfBounds(ContainerFrame:Frame, OtherFrame:Frame) 
	local OtherPos, OtherSize = OtherFrame.AbsolutePosition,OtherFrame.AbsoluteSize
	local ContainerPos, ContainerSize  = ContainerFrame.AbsolutePosition, ContainerFrame.AbsoluteSize

	if OtherPos.X < ContainerPos.X or
		OtherPos.Y < ContainerPos.Y or
		OtherPos.X + OtherSize.X > ContainerPos.X + ContainerSize.X or
		OtherPos.Y + OtherSize.Y > ContainerPos.Y + ContainerSize.Y then
		return true
	end
	return false
end

while task.wait() do
	local check = isOutOfBounds(script.Parent.Parent,script.Parent)
	print(check)
	if userinput:IsKeyDown(Enum.KeyCode.W) or userinput:IsKeyDown(Enum.KeyCode.Up) then
		if not check then
			script.Parent.Position = script.Parent.Position + UDim2.new(0,0,-0.008,0)
		end
	end
	if userinput:IsKeyDown(Enum.KeyCode.S) or userinput:IsKeyDown(Enum.KeyCode.Down) then
		if not check then
			script.Parent.Position = script.Parent.Position + UDim2.new(0,0,0.008,0)
		end
	end
	if userinput:IsKeyDown(Enum.KeyCode.D) or userinput:IsKeyDown(Enum.KeyCode.Right) then
		if not check then
			script.Parent.Position = script.Parent.Position + UDim2.new(0.008,0,0,0)
		end
	end
	if userinput:IsKeyDown(Enum.KeyCode.A) or userinput:IsKeyDown(Enum.KeyCode.Left) then
		if not check then
			script.Parent.Position = script.Parent.Position + UDim2.new(-0.008,0,0,0)
		end
	end
end

i tested it out and it works, but i cant move after i touch the borders, is there a way to fix this?

i did some more test and still struggled : (

is there anyway to fix it

ive made this for a dragable map and checked when the map goes out of the border to reset the position to whats max allowed

	if map.Position.Y.Scale < -1 then
		map.Position =  UDim2.new(map.Position.X.Scale, map.Position.X.Offset, -1, map.Position.Y.Offset)
	end
	
	if map.Position.Y.Scale > 0  then
		map.Position =  UDim2.new(map.Position.X.Scale, map.Position.X.Offset, 0, map.Position.Y.Offset)

	end
	
	if map.Position.X.Scale < -1 then
		map.Position =  UDim2.new(-1, map.Position.X.Offset, map.Position.Y.Scale, map.Position.Y.Offset)
	end

	if map.Position.X.Scale > 0  then
		map.Position =  UDim2.new(0, map.Position.X.Offset, map.Position.Y.Scale, map.Position.Y.Offset)

	end

how does this work if i must ask

1 Like

simple if the position y scale < - 1 then it sets the position in this game map map is the object you need to position to thier position valeus but y scale be set to -1 so it not goes out of the border wjere -1 is y scale - border

1 Like

sorry to reply to late, i had school but how can i implement this system in my frame to make it not go outside the frame?

Could you show me the workspace and frames position and sizes