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

How to make a frame that moves along the x axis?

Hello, I wanted to make a script that uses the "E" button to move the frame. Unfortunately, something went wrong. Does anyone know how to reposition the x or y of the frame?

-- loop,
while true do
--when pressed D
	game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
	    if inputObject.KeyCode == Enum.KeyCode.D then
	        script.Parent.Player.Position.X = script.Parent.Player.Position.X+1 -- change position X of the player(player is a frame)
	    end
	end)

end

1 answer

✓ Accepted answer

Your loop is useless in this case, :Connect creates a connection which gets invoked every time you press a key, you don't need to connect it repeatedly (see this for more info). You can't change X property directly, you need to create new UDim2:

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
  if inputObject.KeyCode == Enum.KeyCode.D then
    script.Parent.Player.Position += UDim2.new(0, 1, 0, 0) -- change position X of the player(player is a frame)
  end
end)

Prefer using :Connect with uppercase "C" as the lowercase variant is deprecated.

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